Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export SQL query result to CSV

I'm using sqlcmd to export a query result with two columns to csv. The simple query is:

SELECT DISTINCT
    CustomerGuid, CustomerPassword
FROM
    ServiceOrder
ORDER BY
    CustomerGuid

When I open the exported csv in Excel both customer and password are on the same column. Is it possible to split them into their own column using sqlcmd. My sqlcmd looks like

SQLCMD -S . -d BAS -Q "SQL STATEMENT" -s "," -o "c:\data.csv"

Thanks.

like image 697
Morten Laustsen Avatar asked Oct 20 '22 21:10

Morten Laustsen


2 Answers

The problem is that you are using , instead of ; as the line delimiter. Try:

SQLCMD -S . -d BAS -Q "SQL STATEMENT" -s ";" -o "c:\data.csv"

:)

like image 113
andy Avatar answered Oct 27 '22 11:10

andy


Actually, this is more of an Excel question and it's already answered in superuser. The default separator when you open a CSV file is locale dependent. In locales where , is a decimal separator, the default is ;.

You can either modify the List separator in Regional settings (not recommended) or open an empty worksheet and import the data, specifying the separator you want.

By the way, the same rules are used in Excel and SharePoint Formulas, where you may have to type ; instead of , to separate values depending on your locale.

like image 44
Panagiotis Kanavos Avatar answered Oct 27 '22 11:10

Panagiotis Kanavos