Convert flatfile column to Datetime

后端 未结 2 1462
既然无缘
既然无缘 2020-12-22 06:01

Can anyone please help me, I have the following format that is coming in from a flatfile.

2013-12-30 00-20-18,114

I have to convert this as datetime

相关标签:
2条回答
  • 2020-12-22 06:53

    You're starting with a string of 2013-12-30 00-20-18,114 and you need to get it into a format SQL Server can handle. The unambiguous format would be 2013-12-30T00:20:18.114 but the Expression language isn't going to like the T there in the middle so we'll have to settle for 2013-12-30 00:20:18.114 as our end state.

    Your problem then becomes, how do I manipulate the supplied string into that format. You can use a script task but there's no need to pull out the big guns for something like this. Because the Expressions in SSIS can be a bit of a pain to work with, I'm going to have several Derived Columns in my data flow to allow me to take small steps toward my end goal

    enter image description here

    OLE_SRC Date String

    I have a basic query here to inject the supplied value

    SELECT
        '' AS wtf
    

    DER Split to parts

    This Derived Column is going to create two columns: DatePart and TimePart. The end result is that I will split this column where the whitespace is.

    DatePart is defined as SUBSTRING(wtf,1,10) so this strips the first 10 characters out of the string. TimePart is defined as LTRIM(RIGHT(wtf,LEN(wtf) - 10)) so it takes the remaining characters, sans leading whitespace.

    DER Standardize

    In this step, I'm going to make the time part into a standard format. I create a new column StandardTime and use the expression REPLACE(REPLACE(TimePart,",","."),"-",":")

    DER Concat

    Here I put the two columns back together again as a shiny new column called ConversionReady. This expression is DatePart + " " + StandardTime

    Data Conversion

    Here I cast my ConversionReady column to date [DT_DATE] datatype and call it Copy of ConversionReady because I'm lazy.

    OLE DB Destination

    I created a table and mapped my wtf to SourceValue and "Copy of ConversionReady" to ConvertedValue.

    CREATE TABLE dbo.ConversionReady
    (
        SourceValue varchar(30)
    ,   ConvertedValue datetime
    );
    

    What is up with all those derived columns? Doesn't that slow thing down?

    No, not really. It certainly makes it easier to debug than trying to do it all in a single expression since you have no debugging capabilities for Expressions.

    0 讨论(0)
  • 2020-12-22 06:59

    I had the same problem when i mixed Date and Time as above in a string and then i tried to convert it in to DateTime. It was not possible.

    Then i did it seperatlly. And added the time in to date using Date Add function.

    I answered my own question please check it below link.

    Feel free to ask any thing. Here is a Link!

    0 讨论(0)
提交回复
热议问题