0

Possible Duplicate:
SQL Server Bulk insert of CSV file with inconsistent quotes

I have been given a million row CSV to import into SQL Server that is basically like this:

"A",B,C,D,"E","F"

I have done it ok in the past using OPENROWSET BULK and a format file catering for CSV file with no quotes around any data or all columns with quotes.

What do people normally do to handle a CSV like this and a FormatFile where some have quotes round and some fields dont?

Note: i will be cross joining with another table and not importing all coumns from the CSV, which is why I used OpenRowset Bulk (and I dont have access to bcp on the server either).

Community
  • 1
  • 1
DomBat
  • 1,981
  • 5
  • 27
  • 42

2 Answers2

2

The solution offered in SQL Server Bulk insert of CSV file with inconsistent quotes suggests preprocessing the file. Given your example, you could use the following update statement to correct the data, however this will only work if the quotes are not surrounding data that contains commas.

UPDATE  @table
SET     value = CASE WHEN LEFT(value, 1) = '"'
                          AND RIGHT(value, 1) = '"'
                     THEN SUBSTRING(value, 2, LEN(value) - 2)
                     ELSE value
                END
Community
  • 1
  • 1
Carl
  • 324
  • 1
  • 15
0

You can first find and replace all double-quotes in the text file with nothing.Then you can do the normal bulk insert with comma(,) as delimiter.

AnandPhadke
  • 13,160
  • 5
  • 26
  • 33