How to Export SQL query output to CSV file with and Without header?

bcp or sqlcmd commands can be used to achieve this.

####SQLCMD - With header:

All names in CAP need to be defined according to your environment /db.

sqlcmd -S SERVERNAME -d DATABASE_NAME -E -o "c:\EXPORTED_CSV_FILE.csv" -Q "select * from TABLENAME" -W -w 999 -s","

####SQLCMD - Without header:

sqlcmd -S SERVERNAME -d DATABASE_NAME -E -o "c:\EXPORTED_CSV_FILE.csv" -Q "select * from TABLENAME" -W -w 999 -s"," -h-1

####bcp Command:

bcp "select * from DATABASE.SCHEMA.TABLE" queryout  "c:\EXPORTED_CSV_FILE.csv"  -c -t"," -r"\n" -S SERVERNAME -T