Handling NULL Character x00 when Exporting to File Using BCP

Handling NULL Character x00 when Exporting to File Using BCP

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0
This article discusses the behavior of the BCP utility when extracting data from SQL Server to files and more specifically to CSV files.

Imagine that you have the below table:

As you can see, records 2 and 3 contain null values.

Then you export the table contents to CSV using BPC:

bcp “SELECT [code],[description],[price] FROM [TestDB1].[dbo].[product]” queryout “c:tmptestExtract.csv” -c -C 1253 -t ; -S “.SQL2K14” -T

Output:

As you can see on the above screenshot, the output seems correct. The records’ NULL values have been replaced with empty strings.

Now consider that the original table, instead of NULL values has empty strings:

Let’s try again to export the new table contents to CSV using BPC:

bcp “SELECT [code],[description],[price] FROM [TestDB1].[dbo].[product]” queryout “c:tmptestExtract.csv” -c -C 1253 -t ; -S “.SQL2K14” -T


Output:

As you can see, the empty strings have been replaced by NULLs which correspond to the hexadecimal character x00.

From the MSDN Library article on BCP, we read the following:

  • out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string.
It seems from the above example, that the queryout keyword has the same behavior regarding null and empty strings.

Now, if you are going to feed the CSV file to another application for parsing and processing it, if the application does not explicitly handle possible occurrences of the NULL character then most probably an error will be occurred.


To this end, always have in mind the above behavior when extracting data to files using BCP.


Recommended eBooks on SQL Server:

Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.