locked
Line Break Issue when Copying from SSMS 2012 & Pasting into Excel RRS feed

  • Question

  • Bit of a weird issue here...

    With SSMS 2008 R2, when I copy Query Data and paste it the data in Excel, it pastes completely fine into Excel (rows & columns all line up).

    With SSMS 2012, when I copy the exact same Query Data and paste the data in Excel, it creates new line breaks and splits the data between multiple rows.

    (Same issue described @ http://stackoverflow.com/questions/13380779/copy-and-paste-from-sql-server-management-studio-2012-new-line-issue-into-excel)

    Any suggestions or thoughts?

    Monday, February 4, 2013 8:01 AM

All replies

  • It sounds like a new feature in SSMS 2012. You get the carriage return if it is on your data, otherwise you don't get it.

    The solution would be to replace the carriage return in SQL statement.

    select top 10 replace([column], char(10) + char(13), ' ') as [struff] 

    • Proposed as answer by Fanny Liu Tuesday, February 5, 2013 9:35 AM
    • Unproposed as answer by brgnewman Wednesday, February 6, 2013 8:19 PM
    Monday, February 4, 2013 4:28 PM
  • Do you have any QUOTES within that data?

    Because Excel treats double quotes differently. if its a frequent operation you might build a package to export/import data between the tools.


    Satya SKJ, Moderator - SQL Server MVP [Knowledge Sharing Network - www.sqlserver-qa.net]
    Author of SQL Server 2008 R2 Administration cookbook.
    Follow me @sqlmaster.

    • Proposed as answer by Fanny Liu Monday, February 11, 2013 9:15 AM
    • Marked as answer by Allen Li - MSFT Thursday, February 21, 2013 8:45 AM
    • Unmarked as answer by brgnewman Tuesday, June 11, 2013 6:44 PM
    Thursday, February 7, 2013 1:50 PM
  • We have the same issue here. In SQL Server 2008 R2 the results of a query copy over just fine to Excel. The same query run in SQL Server 2012 produces some records that have a line break in the middle of the record. There are no line breaks or quotes in any fields in the record. It also happens if we save the results as a .CSV file.

    Is this a bug in SQL 2012? 

    Thursday, February 21, 2013 10:53 PM
  • Same issue, bump
    Friday, August 9, 2013 9:39 PM
  • Same here. But I have combinations of char(13)+char(10), char(10) and char(13) so it's a nightmare to keep on top of
    Tuesday, November 5, 2013 4:03 PM
  • You can open a Connect bug report:

    https://connect.microsoft.com/SQLServer


    Kalman Toth Database & OLAP Architect
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2016



    Thursday, February 13, 2014 3:58 PM
    Answerer
  • I was having this same issue. I overcame it by thinking of it more from the excel perspective.

    Wrap your columns with line breaks in them with double quotes "

    SELECT col1, '"' + columnWithLineBreaks + '"', col3 FROM table
    This tells excel to keep going until the closing quote for the cell (think CSV).


    Monday, June 9, 2014 6:08 PM
  • The real answer is a combination of the above answers:

    SELECT col1, '"' + replace(columnWithLineBreaks,'"', '""') + '"', col3 FROM table

    This allows for Quotes and doesn't require losing the CR/LFs or LF/CRs.

    Thanks to those above, because I couldn't figure this out until I saw your answers.

    • Proposed as answer by Peter A Grigg Tuesday, June 10, 2014 7:52 PM
    Tuesday, June 10, 2014 7:50 PM
  • bump

    I have this same issue and while Peter's solution is very good. It is only good for a single query. I have HUNDREDS of saved queries and multiple users of the same. I have combed through ALL the options/settings and, obviously, there is nothing there. This is a real PIA mostly because we are dealing with some resultant sets that are in excess of 1mil lines. Does anyone have any idea why this is happening in 2012 and did not happen in 2008? Other than rewriting the many hundreds of queries, does anyone have any idea how to resolve it?

    Wednesday, June 18, 2014 8:50 PM
  • So you are exporting data from SQL Server by means of Copy-Paste from SSMS?

    As a short-term solution stick to SSMS 2008.

    As longer-term solution look at the Import/Export Wizard or even be brave to look at SQL Server Integration Services. (He said, despite that he does not know SSIS himself.)

    Since I often copy queries from trace output or DMVs, I very much appreciate that SSMS 2012 retains the line breaks.


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    Wednesday, June 18, 2014 9:26 PM
  • This does not solve having to rewrite past queries, but going forward, putting the corrective SQL code in a function helps.  whenever you are subsequently SELECT-ing a column you suspect may contain line breaks, like a notes or free-text field, then select the function, with the suspect column as input. 
    Monday, May 11, 2015 9:36 PM
  • This is an old topic, but I think there is an implemented solution for your problem in the query options in the SSMS.

    Actually one can make SSMS to retain line breaks on copy or save.

    It can be done even in the current query window:

    1. Right-click in the query area in SSMS,
    2. choose "Query Options",
    3. in the "Results" tree choose "Grid",
    4. and on the right check "Retain CR/LF on copy or save"

    Now run the query and try to copy&paste the result. 

    You can do it also through a "Tools" -> "Options" menu in the menu bar in SSMS, but if you do so, it will work only in the newly opened query window.


    Best regards divx






    • Proposed as answer by lastpyro Monday, August 5, 2019 6:52 AM
    • Edited by Bartosz Siemasz Saturday, September 7, 2019 10:00 PM
    Wednesday, October 3, 2018 7:34 PM
  • Thank you, Bartosz Siemasz!! That saved my day 
    • Edited by lastpyro Monday, August 5, 2019 6:56 AM added name
    Monday, August 5, 2019 6:52 AM