Search code examples
batch-filetext-filessqlcmd

SQL query batch file to text too much spacing


I have created a .bat file that executes a SQL query and displays the results to a .txt file that notepad opens.

Everything executes fine but when the results are displayed in the .txt file, the rows have too much spacing in between them.

How can I get rid of the extra spacing?

This is the batch code I'm using:

sqlcmd -S SQLServer -i c:\scripts\Query3.sql -U user -P pass -o outputfile.txt
@notepad outputfile.txt

Solution

  • Pass the -W parameter to sqlcmd.exe.

    -W
    This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.

    - http://msdn.microsoft.com/en-us/library/ms162773.aspx