SQL Server CSV file import through Bulk import (not able to parse the column with double quotes and comma values)

264 views Asked by At

I have .csv file to import into table. I am using SQL server 2017, but here my problem is with the data I have like below:

column1           column2    column3 <br>
"12,23,45,67,89", TestData , 04-12-2009

When I am using field terminator as comma(,), from column1 value is dividing into different column value as field terminator is (,). What I am looking here, the values which are in double quotes should be escaped from field terminator.

data looks like this way

1

There are 1 answers

2
El.Hum On

According to https://learn.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-ver15, the option FIELDQUOTE = 'quote_characters' should be specified

    BULK INSERT
       { database_name.schema_name.table_or_view_name | schema_name.table_or_view_name | table_or_view_name }
          FROM 'data_file'
         [ WITH
        (
       [ [ , ] BATCHSIZE = batch_size ]
       [ [ , ] CHECK_CONSTRAINTS ]
       [ [ , ] CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ]
       [ [ , ] DATAFILETYPE =
          { 'char' | 'native'| 'widechar' | 'widenative' } ]
       [ [ , ] DATA_SOURCE = 'data_source_name' ]
       [ [ , ] ERRORFILE = 'file_name' ]
       [ [ , ] ERRORFILE_DATA_SOURCE = 'data_source_name' ]
       [ [ , ] FIRSTROW = first_row ]
       [ [ , ] FIRE_TRIGGERS ]
       [ [ , ] FORMATFILE_DATASOURCE = 'data_source_name' ]
       [ [ , ] KEEPIDENTITY ]
       [ [ , ] KEEPNULLS ]
       [ [ , ] KILOBYTES_PER_BATCH = kilobytes_per_batch ]
       [ [ , ] LASTROW = last_row ]
       [ [ , ] MAXERRORS = max_errors ]
       [ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]
       [ [ , ] ROWS_PER_BATCH = rows_per_batch ]
       [ [ , ] ROWTERMINATOR = 'row_terminator' ]
       [ [ , ] TABLOCK ]
    
       -- input file format options
       [ [ , ] FORMAT = 'CSV' ]
       [ [ , ] FIELDQUOTE = 'quote_characters']
       [ [ , ] FORMATFILE = 'format_file_path' ]
       [ [ , ] FIELDTERMINATOR = 'field_terminator' ]
       [ [ , ] ROWTERMINATOR = 'row_terminator' ]
    )]