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

  • คำถาม

  • 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?

    4 กุมภาพันธ์ 2556 8:01


  • 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] 

    • เสนอเป็นคำตอบโดย Fanny Liu 5 กุมภาพันธ์ 2556 9:35
    • ยกเลิกการนำเสนอเป็นคำตอบโดย brgnewman 6 กุมภาพันธ์ 2556 20:19
    4 กุมภาพันธ์ 2556 16:28
  • 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.

    • เสนอเป็นคำตอบโดย Fanny Liu 11 กุมภาพันธ์ 2556 9:15
    • ทำเครื่องหมายเป็นคำตอบโดย Allen Li - MSFT 21 กุมภาพันธ์ 2556 8:45
    • ยกเลิกการทำเครื่องหมายเป็นคำตอบโดย brgnewman 11 มิถุนายน 2556 18:44
    7 กุมภาพันธ์ 2556 13:50
  • 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? 

    21 กุมภาพันธ์ 2556 22:53
  • Same issue, bump
    9 สิงหาคม 2556 21:39
  • 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
    5 พฤศจิกายน 2556 16:03
  • You can open a Connect bug report:


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

    • แก้ไขโดย Kalman TothEditor 10 พฤศจิกายน 2560 15:35
    13 กุมภาพันธ์ 2557 15:58
  • 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).

    9 มิถุนายน 2557 18:08
  • 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.

    • เสนอเป็นคำตอบโดย Peter A Grigg 10 มิถุนายน 2557 19:52
    10 มิถุนายน 2557 19:50
  • 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?

    18 มิถุนายน 2557 20:50
  • 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
    18 มิถุนายน 2557 21:26
  • 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. 
    11 พฤษภาคม 2558 21:36
  • 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

    • เสนอเป็นคำตอบโดย lastpyro 5 สิงหาคม 2562 6:52
    • แก้ไขโดย Bartosz Siemasz 7 กันยายน 2562 22:00
    3 ตุลาคม 2561 19:34
  • Thank you, Bartosz Siemasz!! That saved my day 
    • แก้ไขโดย lastpyro 5 สิงหาคม 2562 6:56 added name
    5 สิงหาคม 2562 6:52