Question

I'm trying to convert a numeric column with the following defined format: DDMMYYYY to a datetime in a mssql database.

What I tried is to do this with a "derive column".

My idea was to get the year, month and day part as strings, concatenate them and convert them to a DT_DBTIMESTAMP with the following code:

(DT_DBTIMESTAMP)(SUBSTRING((DT_WSTR,8)[field],5,4) + "-" + SUBSTRING((DT_WSTR,8)[field],3,2) + "-" + SUBSTRING((DT_WSTR,8)[field],1,2))

I also tried to do it with DT_DBDATE but it didn't work either.

The column of my table looks like this: [GebDat] datetime

Do you have any idea how I can achieve this result? Or maybe give me a hint, what I was doing wrong in the above approach?

Thanks in advance

Was it helpful?

Solution

The code in my initial question was actually correct.

But what Andrew suggested, that there are some values that cannot be converted correctly, was absolutly right. We had some values that didn't conform to the spec (for example a numeric value 4012012 instead of 04012012 which lead to the SSIS trying to convert 40-12-012 to a date, what obviously fails.

What I'm now doing is to check for the length of the input param first. If it is 8 then do exactly the code in the question, if it is 7 then add a 0 to the day and get the day, month, year accordingly:

LEN((DT_WSTR,8)[field]) == 7 ? (DT_DBTIMESTAMP)(SUBSTRING((DT_WSTR,8)[field],4,4) + "-" + SUBSTRING((DT_WSTR,8)[field],2,2) + "-0" + SUBSTRING((DT_WSTR,8)[field],1,1)) : (DT_DBTIMESTAMP)(SUBSTRING((DT_WSTR,8)[field],5,4) + "-" + SUBSTRING((DT_WSTR,8)[field],3,2) + "-" + SUBSTRING((DT_WSTR,8)[field],1,2))

Thanks Andrew, for your hint! It lead me on the right path :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top