Yesterday's date in SSIS package setting in variable through expression

耗尽温柔 提交于 2019-12-22 18:09:25

问题


I am setting a variable in SSIS package and I'm using this expression:

DATEPART("yyyy", GETDATE())*10000 
        + DATEPART("month", GETDATE())*100  
        + DATEPART("day",GETDATE())

The expression will give me a variable value like 'yyyymmdd'. My problem is that I want yesterday's date.

For example on 11/1/2014 it should be 20141031


回答1:


You can use DATEADD function your expression would be :

DATEPART("yyyy", DATEADD( "day",-1, GETDATE()))*10000 + DATEPART("month",  DATEADD( "day",-1, GETDATE())) * 100 + DATEPART("day", DATEADD( "day",-1, GETDATE()))



回答2:


less code...

CONVERT(varchar(8), DATEADD(dd,-1,GETDATE()),112)



回答3:


This will give yesterday's date

(DT_WSTR, 4) YEAR(DATEADD("day",-1,GETDATE())) +RIGHT("0" + (DT_WSTR, 2) DATEPART("MM", DATEADD("day", -1, GETDATE())),2) +RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2)




回答4:


The following example gives the date yesterday with hours and minutes: 2015-09-06-14-40

(DT_WSTR, 4) Year(dateadd("day",-1,getdate())) +  "-" 
+ ( month(dateadd("day",-1,getdate())) < 10 ? "0" + (DT_WSTR, 4) month(dateadd("day",-1,getdate())):(DT_WSTR, 4) month(dateadd("day",-1,getdate()))) 
+ "-" +( day(dateadd("day",-1,getdate())) <10 ? "0" + (DT_WSTR, 4) day(dateadd("day",-1,getdate())):(DT_WSTR, 4) day(dateadd("day",-1,getdate()))) 
+ "-" + right("0"+(DT_WSTR,4)datepart("hh",getdate()),2) 
+ "-" + right("0"+(DT_WSTR,4)datepart("mi",getdate()),2)



回答5:


(DT_WSTR, 4) YEAR(GETDATE()) +RIGHT("0" + (DT_WSTR, 2) MONTH(GETDATE()),2) +RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2)

Above will also give you date one day before. Main issue issue doing plus or minus gives truncation error in expression.

RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2) 

will not break the expression.



来源:https://stackoverflow.com/questions/26820731/yesterdays-date-in-ssis-package-setting-in-variable-through-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!