Search code examples
bashsqlcmd

Removing hyphens in SQLCMD


Is that a way to remove dashed line --------- in SQL cmd. E.g:

Name  ID
--------
Alex  13
Alice 22

Solution

  • I found a best solution:

    (1)Get the column headers

    sqlcmd -S DEV\SQLEXPRESS -d www -U dbadmin -P Admin123$ -Q "set nocount on;select top 0 * from dbo.www1" -o "C:\export-sql-server-to-csv\20.csv" -s "|"
    

    (2)Remove hyphen line

    findstr /R /C:"^[^-]*$" C:\export-sql-server-to-csv\20.csv > C:\export-sql-server-to-csv\21.csv 
    

    (3)Get data without headers

    sqlcmd -S DEV\SQLEXPRESS -d www -U dbadmin -P Admin123$ -i "C:\export-sql-server-to-csv\12.sql" -s "|" -h -1 -o "C:\export-sql-server-to-csv\22.csv"
    

    (4)Append header and data

    type C:\export-sql-server-to-csv\21.csv C:\export-sql-server-to-csv\22.csv > C:\export-sql-server-to-csv\23.csv