Re: BCP import
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Thu, 21 May 2009 10:30:36 -0700
(blindsey@xxxxxxxxxx) writes:
Trying to import a csv file using BCP.
It's comma-separated and each data-value is enclosed in double-
quotation marks, which BCP insists on importing as part of the field.
How can I tell BCP to ignore the surrounding double-quotation marks?
You need to use a format file where you specify the double quotes as
part of the delimiters. Assuming that the first column is enclosed by
double-quotes, you need a little trick that originally learnt from former
SQL Server MVP Linda Weirzbiecki. Say that you have this table:
CREATE TABLE demo (a varchar(23) NOT NULL,
b varchar(23) NOT NULL,
c int NOT NULL,
d char(8) NOT NULL)
In the data file, the integer column is not enclosed in quotes. The format
file should look like this:
8.0
5
1 SQLCHAR 0 0 "\"" 0 "" ""
2 SQLCHAR 0 0 "\",\"" 1 a ""
3 SQLCHAR 0 0 "\"," 2 b ""
4 SQLCHAR 0 0 ",\"" 3 c ""
5 SQLCHAR 0 0 "\"\r\n" 4 d ""
What happens here is that you define to have a fifth initial empty field
which you don't import, by specifying a 0 in the column-number field of
the format file. (1 stands for the first column and so on. The field for
column names is ignored.)
Note that if the quotes are not used consistently, you are out of luck.
Also, if the data includes "" that should be interpreted as a single
double quote, BCP will import it as "", that is two double qoutes. BCP
is a program that reads a stream or bytes, and is not text-centered tool.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.
- Follow-Ups:
- Re: BCP import
- From: blindsey
- Re: BCP import
- References:
- BCP import
- From: blindsey
- BCP import
- Prev by Date: Re: ntext getting truncated
- Next by Date: Re: Bitwise Exclusive Or in WHERE Statement?
- Previous by thread: BCP import
- Next by thread: Re: BCP import
- Index(es):
Relevant Pages
|