How to convert varchar column into timestamp format with Data Conversion(SSIS Data Flow Item)?

Posted on 2008-11-11
Last Modified: 2013-11-10
I am getting date column in the source file(.csv) like  20080727063005. I need to load this value into Sql Server table as Datetime data type column.(yyyy-mm-dd hh:mi:ss FORMAT)  Hope i can do it with Data Conversion . But i am new to SSIS. So can you please tell me how to solve this problem with Data conversion? or Is there  any other easy method please let me know.
Thanks in advance.
Question by:PKTG
    LVL 15

    Assisted Solution

    DECLARE @dt Varchar(30)

    SET @dt =  '20080727063005'

    SELECT CONVERT(datetime,
           LEFT(CAST(CAST(@dt as BigInt) as varchar(14)),8)+' '+
           SUBSTRING(CAST(CAST(@dt as BigInt) as varchar(14)),9,2)+':'+
           SUBSTRING(CAST(CAST(@dt as BigInt) as varchar(14)),11,2)+':'+
           SUBSTRING(CAST(CAST(@dt as BigInt) as varchar(14)),13,2)
    LVL 75

    Assisted Solution

    by:Anthony Perkins
    Or simply :)

    Declare @Dat varchar(14)
    Set @Dat = '20080727063005'

    Select CONVERT(datetime, STUFF(STUFF(STUFF(STUFF(STUFF(@Dat, 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':'), 120)

    Author Comment

    Thanks for the feedback . I did it with below script
    SUBSTRING(column0,7,2)+' '+
    SUBSTRING(column0,13,2) as STRT_TMST  
    Now i am in the situation to do it via SSIS. I can use this scrip in Exec SQL  task. But Can you please tell me is it possible to do with Data Conversion Task? Thanks.
    LVL 15

    Expert Comment

    I believe you can...but I have not tested it...
    LVL 17

    Accepted Solution

    Use the SSIS Transformation, Derived Column.
     With the derived column you use an expression to parse the string into a datetime string and declare it as a DT_DATE data type. The following screen shot shows the start.

    LVL 17

    Expert Comment

    Here is the expression
    SUBSTRING(DateString,1,4) + "/" + SUBSTRING(DateString,5,2) + "/" + SUBSTRING(DateString,7,2) + " " + SUBSTRING(DateString,9,2) + ":" + SUBSTRING(DateString,11,2) + ":" + SUBSTRING(DateString,13,2)
    Here is the return values
    20080727063005,2008/07/27 06:30:05

    Author Comment

    I am converting flatfile datestring to another flat file with SSIS package. With drived column , i gave the below expression with DT_Date datatype.

    SUBSTRING(DateString,1,4) + "-" + SUBSTRING(DateString,5,2) + "-" + SUBSTRING(DateString,7,2) + " " + SUBSTRING(DateString,9,2) + ":" + SUBSTRING(DateString,11,2) + ":" + SUBSTRING(DateString,13,2)  
    I expect it will return datestring value like 2008-07-22 21:58:36  but it shows 7/22/2008 9:58:36 PM.
    But I need to display like 2008-07-22 21:58:36  . Can you please advise me how to fix this? Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now