Saturday, February 25, 2012

BULK INSERT - FIELDTERMINATOR

Hi, I need to bulk insert a .csv file into a database table. However, the problem I am encounter now is the in one of my field which is nvarchar(50) ... the data in .csv has ',' - comma ... which my FIELDTERMINATOR in BULK INSERT is also comma. It gives me error because the next field is an integer field. How do I solve this problem? I hereby give few data example as follow:

In .csv file:

============

"J2825JA","FEB22,MAR1,8/05 - RE","22FEB05",45,20000

"J2825JB","FEB22,MAR1,8/05 - RE","22FEB06",765,435653

Query

=========

SQL = "BULK INSERT [" & sTableName & "]" & _
" FROM '" & sNewFile & "'" & _
" WITH ( FIELDTERMINATOR = ',', " & _
" FIRSTROW = 1, " & _
" ROWTERMINATOR = '\n' ) "

BTW, this is written in ASP. But I this is DB problem. So I posted here. Thanks in advance.

Personally I never use comma-delimited files because of this sort of thing; there is nothing wrong with the format per se, but it seems like many software programs do not know how to handle it correctly. Is there any way you can have the file supplied in tab-delimited format? SQL Server will handle this for you a lot more gracefully. It will also fixed-width format readily.

If this is not a possibility, Access should be able to import the file as-is. FoxPro will also handle this format. And then you can export it in a SQL Server-friendly format.

No comments:

Post a Comment