RFC 4180:
RFC 4180 defines Common Format and MIME Type for Comma-Separated Values (CSV) Files
. One of the requirements of the RFC 4180
is stated as below. This is the point #7
in the RFC link.
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
SQL Server 2000:
DTS Export/Import Wizard
in SQL Server 2000
seems to conform to the above mentioned standards even though the RFC 4180 itself seem to have been published only on October 2005. I am using the below stated SQL Server 2000 version.
Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
SQL Server 2012:
SQL Server Import and Export Wizard
in SQL Server 2012
does not export the data from table to CSV file according to the standard defined in RFC 4180. I am using the below stated SQL Server 2012 version.
Microsoft SQL Server 2012 - 11.0.2316.0 (X64)
Apr 6 2012 03:20:55
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)
Issue Simulation:
Here is a sample that I ran in both SQL Server 2000 and SQL Server 2012. I ran the below query to create a table and insert few records. The ItemDesc
column has data with double-quotes in it. My intention is to export the data from both these SQL Server versions using their in-built export data wizard and compare the generated CSV files.
CREATE TABLE dbo.ItemInformation(
ItemId nvarchar(20) NOT NULL,
ItemDesc nvarchar(100) NOT NULL
)
GO
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('100338754', 'Crown Bolt 3/8"-16 x 1" Stainless-Steel Hex Bolt');
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('202255836', 'Simpson Strong-Tie 5/8" SSTB Anchot Bolt');
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('100171631', 'Grip-Rite #11 x 1-1/2" Electro-Galvanized Steel Roofing Nails');
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('202210289', 'Crown Bolt 1/2" x 3" "Zinc-Plated" Universal Clevis Pin');
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('100136988', 'Tapcon 3/16" x 1-3/4" Climaseal Steel "Flat-Head" Phillips Concrete Anchors (75-Pack)');
INSERT INTO dbo.ItemInformation (ItemId, ItemDesc) VALUES ('203722101', 'KwikTap 3/16" x 2-1/4" "Flat-Head" Concrete Screws (100-Pack)');
GO
On the DTS Export/Import Wizard
in SQL Server 2000
, I used the below settings to export the data to CSV file. I saved the file under the name SQLServer2000_ItemInformation.csv
.
On the SQL Server Import and Export Wizard
in SQL Server 2012
, I used the below settings to export the data to CSV file. I saved the file under the name SQLServer2012_ItemInformation.csv
.
Here is the comparison between the two files using Beyond Compare. The left side contains the file generated by SQL Server 2000
and the right side contains the file generated by SQL Server 2012
. You can notice that the left side file from SQL Server 2000
contains additional double-quotes to compensate the embedded quotes in the data column. This conforms to the standard specified in RFC 4180
but it is clearly missing from the file generated by SQL Server 2012
Searches on the web:
I searched for this bug on the web and found the following links. Following are the bug reports on Microsoft Connect. All these issues seem to be related to importing a file but nothing about exporting data. All these bugs have been closed as Fixed
.
- SSIS flat file parser does not read Column delimiters embedded in text data
- Flat File Connection Manager not handling Text Delimiters in CSV Files
- Embedded quotes in Flat File Import fails
- BUG: Flat File Connection Manager: multiple-character text qualifier does not load all data
Below post on MSDN blog states that changes have been made in SQL Server 2012 with respect to Flat file source supports embedded qualifiers and a variable number of columns per row
Another post on MSDN blog states the same under the section Embedded Qualifiers
.
Workaround that I know of:
I know a workaround to fix the issue by writing a query that would replace all double-quotes ("
) in my column data with two double-quotes (""
) so that the exported file will end up with correct embedded qualifier data. This would avoid pulling the data directly from the table as it is.
My questions:
I don't know if this issue has been truly fixed in
SQL Server 2012
. Has this issue been fixed only forimporting
files that have embedded text qualifiers andnot
forexporting
data to CSV?Probably, I am clearly doing something wrong and missing the obvious. Could someone please explain to me what I am doing wrong here?
Microsoft Connect:
I have submitted a bug report on Microsoft Connect website to get their feedback. Here is the link to the bug report. If you agree that this is a bug, please visit the below link to vote up on Microsoft Connect
website.
Embedded text qualifier during export to CSV does not conform to RFC 4180