user974047
user974047

Reputation: 2285

BCP SQL Command to .csv file has formatting issue

I have a simple BCP command to query my MSSQL database and copy the result to a .csv file like this:

bcp "select fullname from [database1].[dbo].employee" queryout  "c:\test\table.csv"  -c -t"," -r"\n" -S servername -T

The issue comes when the fullname column is varchar separated by a comma like "Lee, Bruce". When the result is copied to the .csv file, the portion before the comma (Lee) is placed into the first column in Excel spreadsheet and the portion after the comma (Bruce) is placed in the second column. I would like it to keep everything in the first column and keep the comma (Lee, Bruce) . Does anyone have any idea how to achieve this?

Upvotes: 0

Views: 4094

Answers (1)

Peter L.
Peter L.

Reputation: 7304

Obviously you should set columns separator to something different than comma. I'm not familiar with the above syntax, but I guess these: -c -t"," -r"\n" are column and new line separators respectively.

Further you should either change default CSV separator in regional settings OR use import wizard for proper data placing in Excel. By the way, there are plenty of similar questions on SO.

Upvotes: 2

Related Questions