Old ladyHow Big Is Your Package?
Size up and convert your DTS package to SSIS. If you have the biggest DTS package, win a gift card and certificate of a world record package size.  
Expression to Format Dates to Two Digits - Brian Knight

Pragmatic Works

Welcome to Pragmatic Works Sign in | Join | Help
in Search

Brian Knight

Expression to Format Dates to Two Digits

When you're creating a flat file, often times you'd like to place a date stamp at the end of the file name in order to show when the file was created simply when sharing the file with a partner. The problem comes in though where you wish to have a two digit date. If you were to create an expression that resembles the following code: 

"C:\\Extract" +  SUBSTRING((DT_WSTR, 30)  GETDATE(),1,10) + ".csv"

The leading zeros in one digit date (ie January with a 01) may yield you the results: C:\Extract2008-2-5.csv (results may vary based on your locale). Your partners may be a frustrated with you when sometimes you send them files with 6 digit dates and other times 8 digit dates since they're trying to automate their processes as well.

The alternative is to first cast the getdate() function into a DT_DBDATE, which only holds the date (stripping the time) and then cast it into a string thereafter.

"C:\\Extract" +  (DT_WSTR, 10) (DT_DBDATE) GETDATE() + ".csv"

This will product a predictable format for your flat files of the following:

 C:\Extract2008-02-05.csv

-- Brian Knight

Published Feb 06 2008, 12:12 AM by Brian Knight
Filed under: ,

Comments

 

jamiet said:

Nice one. I never knew about that. Personally I usually want time precision as well so I have to settle for some fancy string manipulation...but this is cool.

"Your partners may be a frustrated with you when sometimes you send them files with 6 digit dates and other times 8 digit dates "

Frustrated isn't the word. I would go absolutely ballistic if someone sent me files like that. Its stupid mistakes like that that can manifest themselves in so many different ways.

I always like to point to this article at this point: www.codinghorror.com/.../000931.html

-Jamie

February 5, 2008 10:08 PM
Powered by Community Server (Non-Commercial Edition), by Telligent Systems