2

I have a csv that looks like this:

"blah","blah, blah, blah
ect, ect","column 3"
"foo","foo, bar, baz
more stuff on another line", "another column 3"

Is it possible to import this directly into SQL server?

0

1 Answer 1

7

Every row in your file finishes with new line (\n) but the actual rows you want to get finishes with quotation mark and new line. Set ROWTERMINATOR in BULK INSERT command to:

ROWTERMINATOR = '"\n'

EDITED: I think the bigger problem will be with commas in the text. SQL Server does not use text enclosures. So the row will be divided on commas without checking if the comma is inside quotation marks or not.

You may do like this:

BULK INSERT newTable
FROM 'c:\file.txt'
WITH
(
    FIELDTERMINATOR ='",',
    ROWTERMINATOR = '"\n'
)

This will give you the following result:

col1  | col2                                        | col3
----------------------------------------------------------------
"blah | "blah, blah, blah  ect, ect                 | "column 3
"foo  | "foo, bar, baz  more stuff on another line  | "another column 3

All you have to do is to get rid of the quotation marks on the beginning of each cell.

For example:

UPDATE newTable 
SET col1 = RIGHT(col1,LEN(col1)-1), 
    col2 = RIGHT(col2,LEN(col2)-1), 
    col3 = RIGHT(col3,LEN(col3)-1)

I think you can also do this using bcp utility with format file

Sign up to request clarification or add additional context in comments.

2 Comments

won't that cause problems if it doesn't see the last " as the text enclosure?
This was the answer to my problem, however i was using the SQL Server import and export Wizard. My Row delimiter needed to be "{CR}{LF}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.