Last sunday of previous week in SSIS - ssis

I want a SSIS expression which will give me the sunday date of last week.
Corresponding sql server query looks like :
select convert(date,DATEADD(wk, DATEDIFF(wk, 6, convert(date,#report_dt)), -1))
I want the same result in SSIS expression.
Thanks

Assuming your date is held in a variable called report_dt, this works for me:
(DT_DATE)LEFT((DT_WSTR, 30)DATEADD("Day", -1 * (DATEPART("Weekday", #[User::report_dt]) + 7), #[User::report_dt]), 10)
However not sure if this is region safe - but it works in the UK.

This will give you the Sunday of last week using the variable report_dt:
(DT_DBDATE)DATEPART("dw",#[User::report_dt]) == (DT_DBDATE)1 ? #[User::report_dt] : DATEADD("day",-(DATEPART("dw",#[User::report_dt]) - 1),#[User::report_dt])
It's casted as DT_DBTIME so only the date is returned.

Related

IsWeekEnd Expresion in SSIS that is locale-neutral

How can I write an SSIS expression that determines whether a date is a weekend day?
I have this expression, but it seems it only 'works on my machine'
DATEPART( "Weekday", #[User::CurrentDate] ) ==6 || DATEPART( "Weekday", #[User::CurrentDate] ) ==7
After I deploy SSIS package to server, is suddenly changes order of numbers and
Monday became 2.
When on my computer Monday is 1, exactly as I expected.
I checked SQL Server itself, but it seems is something inside SSIS decide if Sunday or Monday is first day.
I cannot find exact question related to this issue.
Well, if you must do this in an SSIS expression, here is one way to work around the problem: you would compare the DW of your variable to the DW of a known constant (like August 3, 2019, which is a Saturday in any locale that I know of).
In pseudocode, IF ( DW(DateVariable) = DW("20190803") OR DW(DateVariable) = (DW("20190803")+1)%7 ) THEN {DateVariable is a weekend}
Personally, I would look for a way to do this in TSQL. Import the raw data "as is" into a staging table, and then do the transformation while moving the data to the destination table with a stored procedure.
DATEPART("DW",GETDATE()) or DATEPART("WEEKDAY",GETDATE()) works perfectly fine on both SQL server and local.
How are you passing value to #[User::CurrentDate] variable(Please check if same date is set when packages is executed on SQL server and on local)
I changed DateTime on my local and tested your expression as part Of Execute SQL task and Derived Column Transformation which gives me 1 for Sunday, 2 for Monday and so on and the result matches with SQL.
SQL query to get day of week:
SELECT GETDATE() Today,DATENAME("DW", GETDATE()) DayofWeekToday,
DATEPART("DW",GETDATE()) DayNumberToday,GETDATE()-1 Yesterday,
DATENAME("DW", GETDATE()-1) DayofWeekYesterday,DATEPART("DW",GETDATE()-1) DayNumberYesterday
Adding more to my answer : There is a difference between DateFirst and DatePart. DateFirst sets first day of the week while Datepart gives you day of the week. use this sql function to check what's your first day of the week
Set Datefirst 7 -- To set first day of week (7 = Sunday)
SELECT ##DATEFIRST -- To check first day of week
**Update your system settings to match first day of the week with sql server and you should get same values when you evaluate expression.
Your expression looks good its only your first day of week on local and sql server are not matching.
Once I updated first day of week on SQL, I was able to replicate the issue you are facing.

SSRS - weekday name

any advice appreciated
I have as a column heading the expression =WeekdayName(weekday(fields!date.value))
This returns the day of the week, however, it is returning a day of the week one day in advance, eg when I put Monday's dates in the parameter it shows as 'Tuesday' in the report.
My question is can the above expression be tweaked to show the WeekdayName one day before, eg =WeekdayName(weekday(fields!date.value -1)) ? I tried this but got an error.
Thanks.
So you want to subtract one day from the your incoming date then you can use the
= DateAdd("d", -1, yourdateField)
This way you can subtract the any number of days from your date.
But did you try to see why it is giving the day of previous date. Do check the system date time or else check with the
=WeekdayName(weekday(Today()))
and see if it gives you the correct day of week for current date.
Weekday and weekdayname functions have both another optional argument for defining the starting day of the week.
Problem is the 2 functions don' t default this argument to the same value so depending on your server settings you should explicitly set the second argument of these functions.
No need to invoke a date function. As the weekday() function returns and integer you can offset the result and use the Mod operator to keep it in bounds:
=WeekdayName((weekday(fields!date.value)+5) Mod 7)+1)
The parenthesis are important to ensure the addition comes first.
Just for reference:
OP asked again because of the weekday offset and this is the correct provided solution.
=WeekdayName(weekday(Today())) gives me tomorrow
=WeekdayName(Weekday(Today(),FirstDayOfWeek.System))

How to get the report between certain date range in Cognos

I have recently started working on Cognos Report Studio. had a T-sql code with 4 table joins. I simply pasted the code in Cognos Report Studio by dragging the SQL toolbox to Query Explorer. The report did run successfully. But now I want this report generated on 1st of every month with maturity date falling between 1st to 30th/31st of that month.
For Eg: If I get a report on 1st May, It should give records of data where the maturity date range is between 1st may to 31st may.
I tried adding the below code to my already written SQL code:
WHERE
CURR_MATURITY_DATE BETWEEN (DATEADD(MM, 0, GETDATE()), 0) AND (DATEADD(MM, 0, GETDATE()) +1, 0) -1)
This code doesn't work.
Pl Note: THe format of column CURR_MATURITY_DATE is: mm/dd/yyyy. Please advise what changes are required in the code to run successfully.
Cognos has an add months function. (At least, Cognos 10 does.) In your expression editor, on the functions tab, it's under Business Date/Time Functions. It's called _add_months. So your function would end up being something like between (_add_months(1,current_date))
You can also use the SQL Server function. You have to put those text parameters (like MM) in curly brackets. So you would end up with dateadd({MM},0,current_date).
For the first day of the current month, in TSQL you would use:DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
In Cognos, the syntax would be DATEADD({MONTH}, DATEDIFF({MONTH}, 0, GETDATE()), 0)
Try these in your WHERE clause:
For first day of the month: select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
For last day of the month : select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate() )+1, 0))
Find it here, it explains all.

find the day example saturday from date in ssis

I want to find out the day of the week from date in ssis.
Actually i want to move the data from one server to another,
In the source server there is one cloumn reportdate having the previous date,so while i wan to copy in to the destination server wan to insert todays date,but there is one case like there is no data comes on sunday in the source server in that case i will have the data of saturday so in this case i want to update the date in the destination server with 2 days plus,please let me know how can i reslove this using ssis.i'm using derived column for manipulating the date column DATEADD("day",2,reportdate) : DATEADD("day",1,reportdate),
so first part will update the date plus 2 of the sourcedate into the detsination table date but how will i find the day of week means when the saturday comes...please let me know how can i reslove this using ssis.
Thanks is advance..
Try this :
DATENAME(weekday, GETDATE())== "Sunday" ? DATEADD("day",2,reportdate):
DATEADD("day",1,reportdate)
Use GETDATE() else use DATEADD(day,-1,reportdate) in the above expression
Update :
Use DATEPART in SSIS
DATEPART( "Weekday", getdate())
Expression is
DATEPART("weekday", GETDATE()) == 1 ? DATEADD("day",2,getdate()):DATEADD("day",1,getdate())
The above expression works in my system having SSIS 2008

How to get current month name in SSRS?

I need current month name as default perameter name in my ssrs report. How can I get current month name using an ssrs expression?
For example "27-09-2012" would become "September"
and one more i need....
27-09-2012 to previous month name as well (August)
First question:
=MonthName(Month(Fields!datefield.Value))
Second question:
=MonthName(Month(DateAdd("m", -1, Today())))
I think the second question answer might be something like that, first converting the date to month then subtracting 1 from the month value and then converting it to month name.
Further reading:
SSRS Reports get Month name from Month Index or from date
Converting month number to month name in reporting services
OFF: I would change the date format you are using to 2012-09-27 as it works in every setting and should give you peace of mind when converting date formats.
Don't subtract 1 b/c it won't work for January.
Use this:
MonthName(Month(DateAdd("m", -1, CDate(Today))))
As a note, I tried the suggestion of =MonthName(Month(today())). What I would get is #error for whatever field the expression was in. However, =MonthName(str(Month(today()))) worked for me. I am unsure of whether or not the MonthName method changed to require a string or if it is some issue with my program. Just figured I would post this in case anyone else was having the same issue.
For Previous Month i found universal way : =MonthName(Month(CDate(Today()))-1,False) for SEPTEMBER (Full Month Name) 'OR'
=MonthName(Month(CDate(Today()))-1,True) for SEP (Short Month Name)