Solved

BULK IMPORT German date format to English SQL Server

Posted on 2007-12-02
9
1,826 Views
Last Modified: 2013-11-06
Hi experts,

I'm trying to import an MS Excel generated CSV into MS SQL 2005 Express.
The CSV contains some data in German data format, e.g. 13.01.2008 (compared to 01.13.2008 US English). The corresponding field in the SQL 2005 destination table is defined as "DateTime".

My sql statemement for running a bulk import looks like that:
BULK INSERT SalesData FROM C:\import.csv WITH (FIELDTERMINATOR = ';', ROWTERMINATOR = '\r\n', TABLOCK, FIRSTROW = 2, KEEPNULLS, CODEPAGE='ACP')

This works fine as long as all data is in English date format. But as I said: my data is in German date format, and I receive the following error message: [...] Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)". The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error. Cannot bulk load because the maximum number of errors (10) was exceeded. Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 16, column 15 (myDateField). [...]

What can I do? Is there any way to tell SQL Server to convert the German date format to US date format before importing?

Thanks so much for any help!
Ingmar
0
Comment
Question by:softwarea
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:deroby
ID: 20391638
maybe you could load the data first into a staging table where the datefields are replaced by varchar fields. Once the table is loaded, copy the data and use the Convert(datetime, <date-string>, 104) for each field to convert... bit more work, but should work nicely...
0
 

Author Comment

by:softwarea
ID: 20394017
Hi deroby,
thanks for your help. Sorry, I should have been more precise: I'm looking for a "direct" solution without staging tables or source file manipulations. I was just hoping I missed someting about SQL Server or the bulk insert statement. If you (or anybody else) do not have any other idea I have no chnace but doing exactly what you suggested.
Thanks again!
Ingmar
0
 
LVL 11

Expert Comment

by:deroby
ID: 20394799
I'm not too familiar with the BULK INSERT statement, but I fear it is rather 'strict' about (a lot of) things, just like bcp.exe

Maybe you could try to 'fiddle' with the 'formatfile', but so far I've not been able to 'tweak' a datetime column into any particalar format :

=> bcp database..table format nul -T -c -x -f myformatfile.xml

creates the .xml file, but I do not see how I can tell it to use convert(datetime, <data>, 108) instead of whatever is the current default...

That said, would it help if you put SET LANGUAGE / 'reset' LANGUAGE around the BULK INSERT statement ??

DECLARE @prev_language

SELECT @prev_language = @@language
 

SET LANGUAGE 'Deutsch'
 

BULK INSERT etc..
 

SET LANGUAGE @prev_language

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 20396411
Please run statement below before issuing the BULK INSERT command.
SET DATEFORMAT dmy

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:softwarea
ID: 20396765
Hi Scott,
YES, that's it. Perfect!!! Thanks so much.

@deroby: You made every effort to help me, and maybe your solution works too. I didn't try it though 'cause Scott's was a little easier to implement. So, thanks again for your help!!!
0
 
LVL 11

Expert Comment

by:deroby
ID: 20397334
grin, imho

SET DATEFORMAT dmy

or

SET LANGUAGE 'German'

is about the same thing, effortwise =)

But never mind, I'm glad it works... Good to know that BULK INSERT "listens" to this setting ... I rather learn than get points .. I think =P
0
 

Author Comment

by:softwarea
ID: 20397364
Ups, I'm really sorry, deroby. I apologize for having not tried your solution too. :-(
0
 
LVL 11

Expert Comment

by:deroby
ID: 20397397
like I said : never mind!!  

FWIW : I'll try to be more convincing next time ... like add flashy pictures and give-away gizmo's to my solutions =P
0
 

Author Comment

by:softwarea
ID: 20397412
;-))) Yes please. Can't wait. Shall I think of a new weird question?
Thanks for your effort and sympathy!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

760 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