Line Split Issues when Copying Data from SQL Server to Excel

Problem

Some of our developers noticed an issue a few days after celebrating our successful SQL Server 2012 upgrade. When they run a query in SQL Server Management Studio and copy and paste the results to Excel, each row gets split into many rows in Excel instead of one row like it used to do. They immediately started blaming SQL Server 2012, because no other changes were made, why is this happening?

Solution

The reason behind this issue is hidden within the data. In earlier SQL Server versions, the carriage return (\n\r) wasn’t taken into consideration when copying data from SQL Server to Excel. This behaves in a different way in SQL Server 2012, as it preserves the carriage return (\n\r) and therefore splits the row into multiple rows in Excel when pasting.

Let’s look at an example:

USE [MSSQLTipsDemo]
GO
 
-- Create New Table
CREATE TABLE [dbo]. [CountryInfo](
       [CountyCode] [nvarchar](20) NOT NULL
) ON [PRIMARY]
 GO
 
INSERT INTO [CountryInfo] VALUES 
(' This Record Contains the first        
Country    
JFK');
INSERT INTO [CountryInfo] VALUES 
(' This Record Contains the second        
Country    
LON');
INSERT INTO [CountryInfo] VALUES 
(' This Record contains the third        
Country    
CAI');
SELECT * FROM [MSSQLTipsDemo].[dbo].[CountryInfo]

When you run the SELECT statement in SQL 2008 R2 and then copy the results to Excel it gets copied as follows:

Run the SELECT statement in SQL 2008 R2, copying it to EXCEL

However, copying the same result set from SQL 2012 to Excel does the following:

Copying the same result set in SQL 2012 would show different results in EXCEL

Replacing the carriage return char(13) and the line feed char(10)

To avoid such issues, you need to clean the data by replacing the carriage return (char(13)) and line feed (char(10)) in your SELECT statement using the following query:

SELECT replace(replace(CountyCode, char(10), ''), char(13), '') 
FROM [MSSQLTipsDemo].[dbo].[CountryInfo]

And then our results end up as expected as shown below:

You need to clean the data by replacing the carriage return (char(13)) and line feed (char(10))

Next Steps

  • Let’s trust our technology; the data is cleaner now in SQL Server 2012.
  • This leads us to the most important point - “Data Quality”; do we really need this in field x or field y? If yes we keep it as is and use the above workaround to copy our data. Otherwise, you could clean the old data and make sure that newly added data is cleaned.

7 Comments

  1. Hi Tyler,

    Does this happen for every record? Are there any other control characters in the strings?

    Take a look at this article: https://www.mssqltips.com/tutorial/sql-ascii-function/

    There is a function that will print out the ASCII values for each character in a string.

  2. What if this still happens even if I’m using the line feed – Char(10) – and not the carriage return?

  3. This isn’t workable if one WANTS the linefeeds in the cell in Excel. For example, I have a table with a text cell containing a log, with separate lines. I have the option set to allow line breaks in grid result in SSMS, which works fine, and when pasted from SSMS into Excel, I want a multi-line cell. So far, no luck.

  4. This is very help full.
    Thanks for the solution, I also had the same problem and you solution is perfect.

  5. It is NOT necessary to rewrite the query, this behavior is just a setting in newer versions of SSMS.

    Select “Tools – Options” from the SSMS menu and navigate to “Query results – SQL Server – Results to Grid”. Clear the checkbox for “Retain CR/LF on copy or save” to get a multi-line result as a single line.

Leave a Reply

Your email address will not be published. Required fields are marked *