Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string in format yyyyMMdd to date using SSIS expression?

Tags:

ssis

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?

like image 350
Djbril Avatar asked Mar 08 '13 14:03

Djbril


People also ask

How to concatenate strings in SSIS expression?

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.

How do I only get date in SSIS?

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.


2 Answers

Fast Parse is a much more elegant solution

  1. Add a Data Conversion Data Flow Component.
  2. Right click, Show Advanced Editor...
  3. Goto Input and Output Properties
  4. Expand Data Conversion Output and click on the date column
  5. Set FastParse to True in the Custom Properties
  6. Make sure the data type is set to database date [DT_DBDATE]
  7. Run the ssis package again the yyyymmdd dates should flow smoothly from nvarchar to date
like image 175
Louie Bao Avatar answered Sep 28 '22 04:09

Louie Bao


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.

like image 39
Zerotoinfinity Avatar answered Sep 28 '22 04:09

Zerotoinfinity