I am trying to bulk insert from a comma separated CSV file containing double quotes.
Here is an example CSV file which illustrates what I am referring to:
---------------------------
ID,TextField1,TextField2
23,This is some text for this field,This is some other text
34,”This field, contains a comma and double quotes”,”So does this field, as you can see”
99,Sometimes it may not have double quotes,”Sometimes, it suddenly does”
---------------------------
As you can see from above, this is a perfectly acceptable CSV file but unfortunately I cannot control the consistency of the double quotes. The supplier of the data feed only appears to surround a field in double quotes if it contains the comma delimiter.
I have tried implementing a query using the MS SQL BULK INSERT query.
Here is an example:
BULK INSERT TempTable
FROM 'C:\Files\CSV\example.csv'
WITH
(
FORMATFILE = 'C:\Files\CSV\example-format.txt'
)
The problem I have here is the format file. I cannot get it to work on string fields in which some rows have the double quotes and other rows do not.
I managed to find an example on this forum here:
Although this works fine for fields surrounded by double quotes, as soon as you remove the double quotes from one of the fields the bulk insert fails.
So perhaps if anyone familiar with format files can help then I may have a solution.
Here is an example CSV file which illustrates what I am referring to:
---------------------------
ID,TextField1,TextField2
23,This is some text for this field,This is some other text
34,”This field, contains a comma and double quotes”,”So does this field, as you can see”
99,Sometimes it may not have double quotes,”Sometimes, it suddenly does”
---------------------------
As you can see from above, this is a perfectly acceptable CSV file but unfortunately I cannot control the consistency of the double quotes. The supplier of the data feed only appears to surround a field in double quotes if it contains the comma delimiter.
I have tried implementing a query using the MS SQL BULK INSERT query.
Here is an example:
BULK INSERT TempTable
FROM 'C:\Files\CSV\example.csv'
WITH
(
FORMATFILE = 'C:\Files\CSV\example-format.txt'
)
The problem I have here is the format file. I cannot get it to work on string fields in which some rows have the double quotes and other rows do not.
I managed to find an example on this forum here:
Although this works fine for fields surrounded by double quotes, as soon as you remove the double quotes from one of the fields the bulk insert fails.
So perhaps if anyone familiar with format files can help then I may have a solution.