0

I am having an issue with setting a double quote (") text delimter to my .csv file using BCP.

Take for example the current SQL script;

 declare @sql varchar(8000)select @sql = 'bcp dmP.pdiStb.vTest2 out  
 c:\bcp\test001234.csv -c -t"\",\""  -T -S'+             @@servername

 EXEC master..xp_cmdshell @sql

When I execute the above, I get a CSV file that looks like the following;

55","sql_gmi_srv","Policy_RenewalDueDate

The above is missing a double quote at the start and end of the file, is there a solution to solve this?

dawsonz
  • 33
  • 3
  • 1
    `-t` defines the field terminator; that's the delimited that goes **between** each column. `-t "\",\""` would mean to put the string `","` between each column, so the values `'a'`, `'b'` would result in the value `a","b`, which is exactly what you're getting here. – Thom A Nov 16 '20 at 10:51
  • Does this answer your question? [SQL Server BCP: How to put quotes around all fields?](https://stackoverflow.com/q/2061113/2029983) – Thom A Nov 16 '20 at 10:54
  • `bcp.exe` itself doesn't handle quoting of field data that contains line breaks, commas or quotes. You can try to hack around it by using a bcp query with quotename(columnname, char(34)) on every column in the table but it's usually easier to use a different tool, such as the `Export-Csv` cmdlet in PowerShell. – AlwaysLearning Nov 16 '20 at 11:24

0 Answers0