简体   繁体   中英

SSIS - Script Component for Date Formatting - VB.NET/C#

so I have a situation where (via SSIS) I'm basically running a SELECT against my DB, and then exporting the results to a .txt flatfile.

The SSIS flow is as follows: OLEDB SOURCE (my select query) -> DATA CONVERSION (change all values to DT_WSTR) -> FLAT FILE DESTINATION

However, I was told I need to change the format of all the datetime column values I export from the format of yyyy-mm-dd hh:nn:ss (what it is in the DB) to mm/dd/yyyy hh:nn:ss AM/PM

From my searching on this topic, I believe a script component between my DATA CONVERSION and FLAT FILE DESTINATION is what I need in SSIS....but I have no VB.NET or C# skills whatsoever. And despite searching, I have yet to really understand how I'd code this date formatting conversion up.

Help?

You can do the conversion in your SELECT statement.

This will get your close:

SELECT CONVERT(VARCHAR(20),GetDate(),101) + ' ' 
+ LTRIM(RIGHT(CONVERT(VARCHAR(26),GetDate(),9),14))

Just replace GetDate() with your date field. If you need it without the nanoseconds, it's a bit longer:

SELECT CONVERT(VARCHAR(20),GetDate(),101) + ' ' 
+ LTRIM(LEFT(RIGHT(CONVERT(VARCHAR(26),GetDate(),9),14),8)) + ' ' 
+ RIGHT(CONVERT(VARCHAR(26),GetDate(),9),2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM