How to bcp export file with encoding ascii

09 December,2020 by Tom Collins

Question: I have a bcp command exporting a sql query which outputs to a file . The problem is the file is being saved as Unicode type - causing a subsequent ETL process to fail. How can I fix? I want the file to export as ascii encoding

bcp "select * FROM myTable" queryout E:\mytable.csv -w -t, -T -S SERVER\MYINST1

 

Answer:  The quick answer is to     ask you to add the -c and -C switches.     From SQL Server Books Online

"-c
Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w."

"-C { ACP | OEM | RAW | code_page }
Specifies the code page of the data in the data file. code_page is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32."

You will need to replace trhe -w with -c - as they are not compatible switches.    As a result your bcp statement should look more like:

bcp "select * FROM myTable" queryout E:\mytable.csv -c -C 1252 -t, -T -S SERVER\MYINST1

Read more about encoding 

How to specify encoding when creating Powershell text file using Out-file

Export-CSV Powershell (SQL Server DBA)


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to bcp export file with encoding ascii


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer