Search code examples
sql-serversql-server-2005sqlcmd

Remove trailing spaces from fixed length in output file generated by sqlcmd


I have a table in SQL Server 2005 with single column of type varchar(500). Data in the column is always 350 characters in length.

When I run a select on it in SSMS query editor, copy & paste the result set in to a text file, the line length in the file is 350, which matches the actual data length.

But when I use sqlcmd with the -o parameter, the resulting file has line length 500, which matches the max length of varchar(500).

Without using any string functions in select, is there a way to let sqlcmd know not to treat it like char(500)?


Solution

  • You can use the sqlcmd formatting option -W to remove trailing spaces from the output file.

    Read more at this MSDN article.