Posts

Showing posts with the label Query

Export Data as csv file from SQL server using command line tools

In the post we will be discussing about the ways of exporting data from SQL server to csv file. As per the use case the output should have: Header row The seperator of columns should be ",". The file should be encoded as UTF-8. There are two tools for exporting data from SQL Server to csv file. These are sqlcmd and bcp. SQLCMD The command which can be used for this purpose is Sqlcmd -S "<ServerName\InstanceName>" -d "<DatabaseName>" -U "<UserName>" -P "<Password>" -i "<SQLScriptFilePath>" -W -f 65001 -h -1 -s "," -o "<OutputFilePath>" E.g.:  Sqlcmd -S "10.00.00.00" -d "EmployeeDatabase" -U "User1" -P "Password" -i "Script1.sql" -W -f 65001 -h -1 -s "," -o "c:\File.csv" Attributes -W is used to remove all the whitespaces -h -1 is used to remove the header (if header is not remo