Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use the BULK INSERT with a file that has double quotes

T-SQL Hints and Tips

How to use the BULK INSERT with a file that has double quotes

by  landShark  Posted    (Edited  )
You have to setup a format file to process a text file that has quotes around every field. Here are the details:

this is two lines from a sample text file:
"1","2","3","4","5","6","7","8"
"22","23","33","44","45","56","57","58"

There are 8 fields to process. Here is how the BULK INSERT would look:

bulk insert SALESTemp
from '\\sales\sales.txt'
with
(
formatfile='\\sales\sales.fmt'
)

The fmt format file is coded as follows:

8.0
17
1 SQLCHAR 0 0 "\"" 0 dummy1 ""
2 SQLCHAR 0 50 "\"" 1 Field1 ""
3 SQLCHAR 0 0 ",\"" 0 dummy2 ""
4 SQLCHAR 0 50 "\"" 2 Field2 ""
5 SQLCHAR 0 0 ",\"" 0 dummy3 ""
6 SQLCHAR 0 50 "\"" 3 Field3 ""
7 SQLCHAR 0 0 ",\"" 0 dummy4 ""
8 SQLCHAR 0 50 "\"" 4 Field4 ""
9 SQLCHAR 0 0 ",\"" 0 dummy5 ""
10 SQLCHAR 0 50 "\"" 5 Field5 ""
11 SQLCHAR 0 0 ",\"" 0 dummy6 ""
12 SQLCHAR 0 50 "\"" 6 Field6 ""
13 SQLCHAR 0 0 ",\"" 0 dummy7 ""
14 SQLCHAR 0 50 "\"" 7 Field7 ""
15 SQLCHAR 0 0 ",\"" 0 dummy8 ""
16 SQLCHAR 0 50 "\"" 8 Field8 ""
17 SQLCHAR 0 0 "\r\n" 0 dummy9 ""

The format file basically tells the bulk insert to ignore the double quotes. The "FIELDx" are the names of the fields on the database. This works great!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top