2

I'm trying to import some data from a text file into an SQL database in my Visual Studio project. All the columns have imported correctly apart from the date column. I used the following command to import the data:

BULK INSERT T2 FROM 'c:\Temp\Data.txt' WITH (FIELDTERMINATOR = ',')

In the text file i have dates like 02-02-12, 03-02-12, etc but in the database, all rows have been set to 01/01/1900. I thought this might have occured because the date formats are different in the text file comparing to the SQL database, does anyone know how i could import my dates into the database?

Thanks

4
  • BULK INSERT is pretty fussy about dates, what happens if you change the date format to mm/dd/yyyy (use / instead if - to separate date portions)? That should work, but how feasibile is it to update your data file? Commented Jan 18, 2012 at 21:54
  • @Sparky The data is within a text file, and i would have to change over 2000 lines manually :-( Commented Jan 18, 2012 at 22:02
  • well, you have csv data... open it as a csv file in excel and then format the dates in the format you need... Commented Jan 18, 2012 at 22:29
  • Is this a one time INSERT? Like Maneesha, if so, you could open it in Excel and change the date format using Excel formulas. If this is an on-going import though, that solution won't work... Commented Jan 18, 2012 at 22:34

1 Answer 1

1

I run into the same problem using your example in 2008r2. No error but all set to 1900-01-01. Looks it's a quirk in bcp when used to populate DATE field. It behaves this way only for DATE column type

So if you use Date type, change it to Datetime or smalldatetime (both worked for me) and see if it helps. Remember to set dateformat so you have days, months and years properly read. If it is day-month-year (in text file) it should be:

set dateformat dmy
BULK INSERT T2 FROM 'c:\Temp\Data.txt' WITH (FIELDTERMINATOR = ',')

I was able to find one report on it, but no definitive answer why this happens: similar case reported here

Sign up to request clarification or add additional context in comments.

3 Comments

found it as active bug in MS database: link
That command worked perfectly, however now i am wondering, how do i tell SQL to miss a column to import? e.g. i have 6 columns of data separated by commas, how do i only import columns 1,2,4 and 5?
@user1091114 You would have to use format file. See this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.