This below evaluation will give me a date set to the format yyyymmdd as an int.
What I need to do it have the ability in SSIS to offset the day by X days and keep the month and year offset accordingly.
I cant seem to find the answer. Everyone has an example on how to format it liek below, but what if you need to format it with an offset in days, months or years and at the same time keep all parts accurate.
RIGHT((DT_STR,4,1252)YEAR(DATEADD("dd",0,getdate())),4) +""+
RIGHT("0" +(DT_STR,4,1252)MONTH(DATEADD("dd",-1,getdate())),2)+""+
RIGHT("0" +(DT_STR,4,1252)DAY(DATEADD("dd",0,getdate())) ,2)
Anyone?
In C# or TSQL this is easy, but this SSIS is growing my patience thin.
Thanks.
Here is a possible solution that will allow you to achieve this.
In the SSIS package, declare four package scope variables.
TodaysDate – Variable of type
DateTime. When you set to DateTime, the variable will be assigned with current date and time. You can also change this and set it to the date of your choice.OffsetValue – Variable of type
Int32. This will hold the offset value. For this example, I am choosing the offset value to be in terms of days. So, I have set it to a value of 7.OffsetDate – Variable of type
DateTime. Select this variable and press F4 to view the properties. Change the property EvaluateAsExpression toTrue. Set the Expresstion to the valueDATEADD( "dd", @[User::OffsetValue] , @[User::TodaysDate] ). This expressions adds the offset value to the variable TodaysDate so you get the new OffsetDate.FormattedDate – Variable of type
String. Select this variable and press F4 to view the properties. Change the property EvaluateAsExpression toTrue. Set the Expresstion to the following valueRIGHT((DT_STR,4,1252)YEAR(DATEADD("dd",0, @[User::OffsetDate] )),4) +""+RIGHT("0" +(DT_STR,4,1252)MONTH(DATEADD("dd",-1,@[User::OffsetDate])),2)+""+
RIGHT("0" +(DT_STR,4,1252)DAY(DATEADD("dd",0,@[User::OffsetDate])) ,2)
The only difference is you are formatting the date of your choice, here in this case the value in variable
OffsetDate. This variable will hold the date that has already been offset.The below screenshot shows a sample. The variable TodaysDate is set to
11/28/2011. Adding 7 days to the variable sets the field OffsetDate to12/05/2011. As you can see, the variable FormattedDate formats the newly offset date to20111205.This example can also be done with one less variable by eliminating the variable TodaysDate. You can also have one offset variable for each type like days, months and years. It all comes down to ones’ preference.
I hope this is what you were looking for and probably gives you an idea of how to achieve this.