April 14, 2021 at 6:34 pm
I use the below to get Last day of Previous Month, but I want the date format to be MM/DD/YYYY. We are sending that in the output file.
REPLACE(LEFT((DT_WSTR,20)DATEADD("d",-1,DATEADD("m",DATEDIFF("m",(DT_DATE)"1901-01-01",GETDATE()),(DT_DATE)"1901-01-01")),10),"-","/")
Thanks!
April 14, 2021 at 6:47 pm
Lookup the key word CONVERT. You'll need to use the format number of 101.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 14, 2021 at 7:08 pm
You mean like this? This is throwing error. I thought the convert 101 is for T-SQL.
CONVERT(DT_STR,REPLACE(LEFT((DT_WSTR,20)DATEADD("d",-1,DATEADD("m",DATEDIFF("m",(DT_DATE)"1901-01-01",GETDATE()),(DT_DATE)"1901-01-01")),10),"-","/"),101)
April 14, 2021 at 8:28 pm
look at this http://bilearninghub.blogspot.com/2018/05/format-date-in-ssis-mm-dd-yyyy-or.html
RIGHT("0" + (DT_STR,4,1252)DATEPART( "mm" , getdate() ), 2) +"-"+RIGHT("0" + (DT_STR,4,1252)DATEPART( "dd" , getdate() ), 2)+"-"+(DT_STR,4,1252)DATEPART( "yyyy" , getdate() )
as you are doing a bit of calculation with the dates I would advise that that is done on an earlier step and then use that variable/column on the final formatting formula.
April 14, 2021 at 10:04 pm
As Jeff mentioned, CONVERT + 101 gets you the formatting you need:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101);
For the last day of the previous month:
SELECT EOMONTH(DATEADD(MONTH,-1,GETDATE()));
For the last day of the previous month formatted as needed:
SELECT CONVERT(VARCHAR(10), EOMONTH(DATEADD(MONTH,-1,GETDATE())), 101);
-- Itzik Ben-Gan 2001
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply