Using the Save Results As option, shown below.
CSV looks good!
But when Excel opens/imports it:
- Long strings are mangled as numbers (if the data is largely numeric)
- Dates are not detected properly
- My users beef about all this
How do I get a query to a clean excel file (a real xls/xlsx) without dancing? (We like everything to actually in the SQL file itself… I wish we could do "SELECT INTO FILE:C:\SQLOUTPUT\DATA.XLS …")
Best Answer
First, make sure you've got the query options set up the way you want.
Go to the Query menu, and choose Query Options:
The first of the two highlighted options tells SSMS to include the column headers in your CSV file. The second tells SSMS to put single quotes around columns that include a comma.
I suspect you've already done this, but thought I should mention it for completeness.
If this isn't sufficient to get your data into a format Excel will correctly interpret, my next step might be (as noted in the comments) to use the Import/Export Wizard to generate a SSIS package that does this. However, the wizard does not always get the quoting correct - in particular, it can put things in double quotes, but won't "double up" the double quotes embedded in the string.
Generally at that point, I resort to brute force.
I create a modified query that:
"
) with double double quotes (""
);So, instead of
I generate:
If I'm going to use the query regularly, it'll include a
sortOrder
column (set to 1), and beUNION ALL
ed with aSELECT
that creates the header row. I then wrap theUNION ALL
ed queries (making them a subquery), so I can select justOne_Big_Column
, and sort bysortOrder
.You can see this run here.
Note: If you're formatting the CSV manually, you'll probably want both of the check boxes noted above turned off.