Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Bulk Insert from CSV with comma in field values

Author  Topic 

alasse130
Starting Member

21 Posts

Posted - 2011-07-16 : 09:51:38
Hi, I'm currently running into a problem with using BULK INSERT. My CSV file looks like

"1", "john, doe", "0"

I'm using a format file
1 SQLCHAR 0 0 "\",\"" 2 ID SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 1 "\",\"" 2 Name SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 2 "\",\"" 2 IsDeleted SQL_Latin1_General_CP1_CI_AS

Here's my bulk insert statement:
BULK INSERT FROM <filename>
WITH ( firstrow=2,FIELDTERMINATOR=',',datafiletype='widechar',
FORMATFILE=<formatfile> )

This works well if my csv don't contain any fields with commas in it. But if there are data as shown above, it gives me an error:
String or binary data would be truncated.
The statement has been terminated.

Is what I'm doing possible at all? I read from MSDN that data fields can never contain field terminators. But my current implementation is based on this blog (http://lanestechblog.blogspot.com/2008/08/sql-server-bulk-insert-using-format.html) demonstrating exactly how can this be accomplished using format files.

Appreciate your suggestions.

alasse130
Starting Member

21 Posts

Posted - 2011-07-16 : 10:24:13
never mind, It wasn't the bulk insert causing the error.
Go to Top of Page
   

- Advertisement -