Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 661
Abs Avatar asked Jan 13 '10 23:01

Abs


2 Answers

bcp "SELECT char(34) + * +char(34) FROM atable queryout "C:\temp\out.csv" -T -N -c /t"\",\""

This will put quotes before and after each field (including the first and the last).

like image 96
Seb Avatar answered Oct 13 '22 22:10

Seb


Alternatively, if you are fine for Powershell based script, you can try with below code, which does automatic quoting.

Invoke-sqlcmd -ConnectionString "Server=SERVERNAME, `
3180;Database=DATABASENAME;Trusted_Connection=True;"  `
-Query "SET NOCOUNT ON;SELECT * FROM TABLENAME" -MaxCharLength 700 | `
Export-Csv -NoTypeInformation -path C:\temp\FileName.csv -Encoding UTF8
like image 23
Venkataraman R Avatar answered Oct 14 '22 00:10

Venkataraman R