SSMS Results to Grid - CRLF not preserved in copy/paste - any better techniques?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



SSMS Results to Grid - CRLF not preserved in copy/paste - any better techniques?



When I have a result set in the grid like:


SELECT 'line 1
line 2
line 3'



or


SELECT 'line 1' + CHAR(13) + CHAR(10) + 'line 2' + CHAR(13) + CHAR(10) + 'line 3'



With embedded CRLF, the display in the grid appears to replace them with spaces (I guess so that they will display all the data).



The problem is that if I am code-generating a script, I cannot simply cut and paste this. I have to convert the code to open a cursor and print the relevant columns so that I can copy and paste them from the text results.



Is there any simpler workaround to preserve the CRLF in a copy/paste operation from the results grid?



The reason that the grid is helpful is that I am currently generating a number of scripts for the same object in different columns - a bcp out in one column, an xml format file in another, a table create script in another, etc...





+1 This really is a pain in the ass in SQL Server Management Studio.
– Tomalak
Nov 16 '11 at 16:26




4 Answers
4



This issue has been fixed in SSMS 16.5 build 13.0.16000.28 with the addition of an option to preserve CR/LF on copy/save (more details) (Connect bug).



This will cause CR, LF, and CRLF to be treated as newlines when you copy a cell.


CR


LF


CRLF





One more sidenote: I had to restart SSMS after selecting this option before it started working.
– Wouter
Nov 4 '16 at 9:57





This would have saved me hours if I had seen it sooner. I assumed the SSIS package was stripping out my line breaks. I also had to restart SSMS before this change took effect.
– Eric Harlan
Jun 27 '17 at 16:39






For me the changed setting already affected a new query window. No need to restart SSMS.
– JanW
Jul 20 at 10:39





why is this not on by default??
– jtate
yesterday



I believe this article has several techniques which can be useful: http://sqlblogcasts.com/blogs/martinbell/archive/2009/10/25/How-to-display-long-text-in-SSMS.aspx





SELECT CAST([field_with_crlf] AS XML) is the pertinent trick to get a link in an SSMS grid cell to a separate page with the CRLFs intact.. copying rows works, as well.
– zanlok
Mar 26 '13 at 22:36


SELECT CAST([field_with_crlf] AS XML)



One thing you can do is send results to a file, then use an editor capable of watching a file for changes which has superior capabilities for understanding the output.



it is a hack, but try this:



wrap your result set in a REPLACE (.....,CHAR(13)+CHAR(10),CHAR(182)) to preserve the line breaks, you can then replace them back


SELECT
REPLACE ('line 1' + CHAR(13) + CHAR(10)+ 'line 2' + CHAR(13) + CHAR(10) + 'line 3'
,CHAR(13)+CHAR(10),CHAR(182)
)



OUTPUT:


----------------------
line 1¶line 2¶line 3

(1 row(s) affected)



replace them back in SQL:


select replace('line 1¶line 2¶line 3',CHAR(182),CHAR(13)+CHAR(10))



output:


-------------------
line 1
line 2
line 3

(1 row(s) affected)



or in a good text editor.





I appreciate the effort, but this isn't any less work than selecting a single column at a time in text output mode or doing the print with a cursor.
– Cade Roux
Apr 21 '10 at 12:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard