views:

54

answers:

3

I want to cast a date into ISO format, and use it as a string in an expression in SSIS.

This is what I type in T-SQL

select convert(varchar(8), GetDate(), 112)

and this is what I get back

20100630

My goal - is to create an archive folder based on the date. I have the File System Task part of creating a folder sorted, I can do that ... I just need to be able to Cast the date to a string, so I can use that.

Thanks in advance.

+7  A: 

You'll need to add an expression:

RIGHT((DT_WSTR, 4) DATEPART("yyyy", GetDate()), 4) + 
RIGHT("0" + (DT_WSTR,2)DatePart("mm", GetDate()), 2) + 
RIGHT("0" + (DT_WSTR,2)DatePart("dd", GetDate()), 2)

This expression will create the result that you are after.

Ardman
ALMOST correct. that returned 100630, but I changed the ,2 to a ,4 on the first line, and got 20100630, which is exactly what I wanted. So you get a hearty pat on the back, and the accepted answer.
cometbill
@Cometbill: my mistake. Have corrected the answer.
Ardman
+1  A: 

In SSIS you would use the DT_STR or DT_WSTR cast to accomplish. A few examples are

(DT_STR, 4, 1252)YEAR(GETDATE()) + 
RIGHT("0" + (DT_STR, 2, 1252)MONTH(GETDATE()), 2) + 
RIGHT("0" + (DT_STR, 2, 1252)DAY(GETDATE()), 2)

or

(DT_WSTR, 4)YEAR(GETDATE()) + 
RIGHT("0" + (DT_WSTR, 2)MONTH(GETDATE()),2) + 
RIGHT("0" + (DT_WSTR, 2)DAY(GETDATE()), 2)

See the MSDN documentation for more information.

Garett
+2  A: 

To add to the other answers, here is a reference guide I use frequently:

http://sqlis.com/sqlis/post/Expression-Date-Functions.aspx

etliens
excellent. Thank you.
cometbill