I recently upgraded to SQL2012 and am using Management Studio. One of my columns in the database has a CHAR(13) + CHAR(10)
stored in it.
When I was using SQL Server 2008, this would copy and paste completely fine into Excel. Now, however, copying and pasting the same data creates a new line/ carriage return in the data I have in Excel.
Is there a setting I missed in SQL2012 that will resolve this issue? I don't want to simply REPLACE(CHAR(13) + CHAR(10))
on every single database selection, as I would have to go from using SELECT *
to defining each individual column.
I found a workaround for the problem; instead of copy-pasting by hand, use Excel to connect to your database and import the complete table. Then remove the data you are not interested in.
Here are the steps (for Excel 2010)
Data > Get external data: From other sources > From SQL Server
Excel will now import the complete table with the newlines intact.
I ran into the same issue. I was able to get my results to a CSV using the following solution:
nvarchar(max)
field move that field to the bottom of your table.This is fixed by adding a new option Retain CR\LF on copy or save under the Tools -> Options... menu, Query Results -> SQL Server -> Results to Grid.
You need to open new session (window) to make the change take a place.
The default is unselected (false) which means that copying/saving from the grid will copy the text as it is displayed (with CR\LF replaced with spaces). If set to true the text will be copied/saved from the grid as it actually is stored - without the character replacement.
In case people missed following the chain of connect items (leading to https://connect.microsoft.com/SQLServer/feedback/details/735714), this issue has been fixed in the preview version of SSMS.
You can download this for free from https://msdn.microsoft.com/library/mt238290.aspx, it is a standalone download so does not need the full SQL media anymore.
(Note - the page at https://msdn.microsoft.com/library/ms190078.aspx currently isn't updated with this information. I'm following up on this so it should reflect the new option soon)
The best way I've come up to include the carriage returns/line breaks in the result (Copy/Copy with Headers/Save Results As) for copying to Excel is to add the double quotes in the
SELECT
, e.g.:If the column data itself can contain double quotes, they can be escaped by 'double-double quoting':
Empty column data will show up as just 2 double quotes in SQL Management Studio, but copying to Excel will result in an empty cell.
NULL
values will be kept, but that can be changed by usingCONCAT('"', ColumnName, '"')
orCOALESCE(ColumnName, '')
.As commented by @JohnLBevan, escaping column data can also be done using the built-in function
QUOTENAME
:Once Data is exported to excel, highlight the date column and format to fit your needs or use the custom field. Worked for me like a charm!