简体   繁体   中英

SSMS Export Query Results to Excel or CSV

I am trying to export the results of a query to CSV and then ultimately Excel.

My issue is, one of my columns has commas in it and the commas interrupt Excel parsing the CSV in the correct places.

I don't have write privileges to the db, so dropping the query results into a table and then exporting that table to excel is not an option.

Is there a way to export to excel, or at least CSV with a column that has commas in it?

1 Right click your DB in object explorer window, go to Tasks -> Export Data

2 Pick Sql Server native client as your source, put in your connection parameters

3 Pick Excel as your destination

4 On the next screen paste your select query

5 go through the remaining screens making sure things look correct

6 profit

The way I have dealt with this is to use a tab separated file instead (assuming we don't have tabs in the data)

第一步导出tsv

选择 tsv 选项

Another way to try is to copy & paste the table directly from the query results in SSMS into Excel and then saving the file from there.

If you can query the database, you can use something like this to retrieve the data without columns.

DECLARE @String varchar(25) = 'Comman delimited, value.'

SELECT REPLACE(@String, ',', ' ') AS ScrubbedValue

Use a replace function to replace commas with a pipe or other character. In Excel, the pipe can be reverted to a comma using Excel's Replace, if required.

select
 Date     = a.Date_Time,
 Action   = Type,
 Username = a.username,
 Desc     = replace( description,',','|'),  -- replace pesky commas
from
 dbo.AuditLog a
where
 a.Date_Time > DATEADD( DAY, -1, getdate()) 

You can use TOAD for SQL Server. It has export as Excel. This is better then csv because it preserves the data type. This works even in trial mode and after the trial expired. I use Toad just for the export to excel. 在此处输入图像描述

Another option is to use "Azure Data Studio". It's free, and has save as excel option.

在此处输入图像描述

I use SSMS as my main tool, but when I need to export something to excel, I copy and paste the query into azure data studio.

Commas

If you use the solution by @MikaelEliasson , you can actually save to CSV without any issues - SSMS will automatically surround any data containing commas with double quotes. Just right-click, Save Results As... anywhere on the data grid results.

第一步导出tsv

Header row

To include the header row , navigate to Tools → Options → Query Results → Results to Grid and ensure "Include column headers when copying or saving the results" is ticked.

Leading zeroes

If any fields have leading zeroes, Excel will truncate them (even if you convert to text). To avoid this , use "=""MyLeadingZeroField""" , so:

 SELECT CONCAT('"=""', MyLeadingZeroField, '"""')

which will then display correctly once opened in Excel.

If you try the solution of a leading single quote or surrounding by single quotes this doesn't really work as these will display in Excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM