Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sqlcmd -U xx -P xx -S xxx -Q "select * from sometable" -o D:\output.csv -W -w 1024 -s","


In the above sqlcmd command I am using 'comma' as the delimiter to separate the columns in database and to get the output in csv format. But what if we already have 'comma' in between the data not between the columns? How to modify the above statement in this case?

Ex:

A      B
-----  -----
abc    g,h,i
def    jk,l


What I have tried:

Tried to use pipe syambol as the delimiter (
-s"|"
). But it is not giving the output in proper csv format
Posted
Updated 17-Aug-18 3:40am

The sqlcmd utility doesn't have an option to wrap the column values in quotes.

There seem to be two simple options to solve this:


1) Use QUOTENAME in your query to wrap your column values in quotes:
SELECT QUOTENAME(Col1, '"'), ...
sql server - Exporting CSV data using SQLCMD.EXE - Stack Overflow[^]
Your values will be limited to a maximum of 128 characters.


2) Use Powershell, and pipe the results from Invoke-SqlCmd into Export-Csv:
file - How to export data as CSV format from SQL Server using sqlcmd? - Stack Overflow[^]


Alternatively, you could write your own program to execute a query and export the results to CSV, using something like CsvHelper[^] to generate the output.
 
Share this answer
 
Comments
Member 13076177 5-Apr-17 1:44am    
To wrap the column values in quotes, I need to mention the column names explicitly. But as shown in the above example , 'select * from sometable' will be passed as the input query. How to enclose the column names which contains comma inside quotes without mentioning the names explicitly?
Richard Deeming 5-Apr-17 13:34pm    
You can't. You'll either need to specify the column list explicitly, or use one of the other options.
I had a similar issue of how to use SQLCMD export to CSV the results of a query whose data may contain quotes and commas as part of the output. To handle the optional escaping and quoting needs, I knew that my query was going to be too long for inline-inclusion in the call to SQLCMD. I believe the -i option would allow you to put the query in its own file but I opted to use a stored procedure.

I created the following user-defined scalar function:

USE [MyDatabase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Lynn Nicholson
-- Create date: 08/16/2018
-- Description:	Prep a value for output to CSV file
-- =============================================
ALTER FUNCTION [dbo].[CSV] 
(
	@text varchar(4000)
)
RETURNS varchar(4000)
AS
BEGIN
	DECLARE @escaped varchar(4000)
	SELECT @escaped = @text
	
	IF (CHARINDEX(',', @text) + CHARINDEX('"', @text)) > 0 -- if our text has a comma or single quote
	BEGIN
		SET @escaped = CONCAT('"', REPLACE(@escaped, '"', '""'), '"')	-- double-up any quotes in the text and surround with quotes.
	END
	
	RETURN @escaped

END


And given this query:
SELECT firstName, lastName, notes FROM client


You call the prep function like this:
SELECT dbo.CSV(firstName) as firstName, dbo.CSV(lastName) as lastName, dbo.CSV(notes) as notes FROM client


The "dbo." is important.

Use at your own risk and ymmv, etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900