sql data type for date time stamp

Posted on 2012-09-17
Last Modified: 2012-09-25
I've got a csv file that has date/time represented in two columns as:

entrydate: 2012-04-06 11:12:40.000
shipdate: 4/7/2012 12:20:31 PM

I've got my sql table set up as:
[entrydate] [datetime] NULL,
[shipdate] [datetime] NULL,

when i try to import there is a conversion failure on the shipdate. so i switched the data type to a varchar 100 and it loads. What is the better data type for the shipdate column? Should i keep it at a varchar?
Question by:fwstealer
    LVL 19

    Expert Comment

    by:Ken Butters
    I would open the CSV file in Excel... and refomat the ship date into the same format as your entry date... and keep the shipdate as a datetime.

    Or --- even easier... wherever you got the CSV file from... that process should be able to provide you with a consistent date/time format.
    LVL 68

    Accepted Solution

    Use a staging table for the load from the csv, formatted as varchar.  [Even if you change the spreadsheet, it's still best to use a staging table when loading external data.]

    Then when you move data from the staging table to the real/production table, change it to datetime, which is the proper datatype for this type of data.

    Author Comment

    wouldn't it be better to cast the shipdate in the ssis package and load directly to the table? if so how should i do the cast?
    LVL 68

    Expert Comment

    Not for me; I would never use an external file to load directly into a production table.

    But if generally you're willing to do that, then it depends.  If you have an invalid date, do you want the SSIS package to fail, or do you want to continue processing the valid data and just report on the invalid row(s)?

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now