Export from SSMS to Excel using a .bat file. Issue with commas
08:13 22 Dec 2025

I have a .bat file that creates a duplicate of the TblPIF and names it TblPIF_New. It runs the Replace_IC.sql file which places " around the text in a particular field that contains commas and then states the comma as the delimiter.

SQLCMD -S SFT-RESEARCH\RESEARCH -d Research_PIF_Database -E -Q "DROP TABLE TblPIF_New"
SQLCMD -S SFT-RESEARCH\RESEARCH -d Research_PIF_Database -E -Q "select * into TblPIF_New from TblPIF"
SQLCMD -S SFT-RESEARCH\RESEARCH -d Research_PIF_Database -i S:\Research\PIFDatabase\ExportFiles\Replace_IC.sql
SQLCMD -S SFT-RESEARCH\RESEARCH -d Research_PIF_Database -E -Q "select * from [TblPIF_New] ORDER BY ProjectID" -s "," -o "S:\Research\PIFDatabase\ExportFiles\TblPIF_New.csv"

Replace_IC.sql:

UPDATE TblPIF_New
SET ProjectTitle = '""' + CAST([ProjectTitle] AS VARCHAR(MAX)) + '""';

I have tried without the " quote marks and different delimiters. None of them contain each column with commas within themselves.

This is what the field looks like in the SQL db.

How the field looks in the sql database, with the

This is how it is coming out in the .csv file, ie, any with commas, are crossing several columns. Shows "" at the end of the ProjectTitle field but not at the beginning:

How the field is coming out in csv file

How can I get this data out of the database into the respective correct columns, please?

sql export-to-csv