I have a date 20130131
in csv which I'm trying to convert to 2013-13-01
using Derived Column in SSIS. I have used this expression but it does not seem to work.
(DT_DBTIMESTAMP)(SUBSTRING(DATE_DECISION_TO_REFER,1,2) + "-" + SUBSTRING(DATE_DECISION_TO_REFER,3,2) + "-" + SUBSTRING(DATE_DECISION_TO_REFER,5,2))
How do I rewrite this expression to produce the correct output with a value of data type DT_DBTIMESTAMP
?
To concatenate two numeric values, both numeric values must be explicitly cast to a string data type. A concatenation can use only one BLOB data type: DT_TEXT, DT_NTEXT, or DT_IMAGE. If either element is null, the result is null. String literals must be enclosed in quotation marks.
You can use the YEAR, MONTH and DAY funtions to get the different parts of the date, put them together in the string "yyyy-mm-dd" and then convert this to a date. edit: this will be a lot easier if you do the conversion in SQL Server itself. You can use the CONVERT function to convert GETDATE to a date only format.
Fast Parse is a much more elegant solution
As per Kyle Hale suggestion, below is my answer
SUBSTRING([DATE_DECISION_TO_REFER],1,4) + "-" +
SUBSTRING([DATE_DECISION_TO_REFER],5,2) + "-" + SUBSTRING([DATE_DECISION_TO_REFER],7,2)
To educate you so that you will never face this issue again, this is how expression works
Get 4 characters from DATE_DECISION_TO_REFER starting at position 1, add a dash,
get 2 characters from DATE_DECISION_TO_REFER starting at position 5,
add a dash then add 2 characters from DATE_DECISION_TO_REFER starting at position 7.
Hope this will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With