SQL Server BCP: How to put quotes around all fields?

By : Abs
Source: Stackoverflow.com

I have this BCP command:

'bcp DBName..vieter out c:\test003.txt -c -T /t"\",\"" -S SERVER'

The output CSV I get does not put quotes around the field names, instead it puts it around the commas! How can I get the /t"\",\"" to put quotes around all fields.

Thanks all

By : Abs


You need to use CHAR(34) for the quote. This page has more details: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=153000

Setting the row terminator in addition to the field terminator should do the trick

'bcp DBName..vieter out c:\test003.txt -c -T -t"\",\"" -r"\"\n\"" -S SERVER'

This will likely work, but miss of the leading " for the first field of the first line, and perhaps the last of the last, i'm not sure, just guessing really, no server here!

or try using QUOTENAME to wrap text fields (you could also wrap numbers, but that isn't normally required.

'bcp "SELECT id, age, QUOTENAME(name,'"') FROM DBName..vieter" queryout c:\test003.txt -c -T -t"," -S SERVER'

This video can help you solving your question :)
By: admin