Copying formatted text out of a grid column.7
August 27, 2018 by Kenneth Fisher
I was reading a post today by Bert Wagner (b/t).
Displaying Long Variable Values in SQL Server
By the way, if you haven’t seen any of Bert’s vlog posts before they are well done and highly entertaining. Anyway, Bert was having difficulties displaying particularly long text strings. Now I’ve had this problem before myself and used several of the solutions he did and learned a few new ones. However, I did know a handy little setting that solved one of his problems. Keeping carriage returns and line feeds when copying data out of a grid output.
When you put formatted text into a variable:
-- Setup DECLARE @MyString varchar(8000); SET @MyString = 'SELECT * FROM sys.databases WHERE database_id = 4' ; SELECT @MyString;
The last thing you want to get when you copy and paste the output is this:
SSMS Settings to the rescue!
It turns out there is a very simple SSMS setting that will fix this problem!
Now, you do need to be aware that the settings Tools -> Options are just for new query windows. If you want to change the settings for an already existing window then either go to Query -> Query Options or right click in the window and go to Query Options from there.
Either way, once you are done and re-run your script you get this when you copy and paste:
Much better right?
Category: Microsoft SQL Server, Settings, SQLServerPedia Syndication, SSMS | Tags: Microsoft SQL Server, Settings, SSMS
7 thoughts on “Copying formatted text out of a grid column.”
Leave a Reply to Saving Newlines When Copying From SSMS Grids – Curated SQL Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
[…] Kenneth Fisher shows us how to retain CR/LF values when copying and pasting from SQL Server Manageme…: […]
Had no affect
Did you open a new query window or was this an existing one? The settings for existing query windows are in a different place.
Hi Kenneth Fisher,
I have a similar problem but am trying to copy data from an nvarchar(max) field to another nvarchar(max) filed in the same table.
Have tried the solution you suggested. But the new field does not keeping carriage returns and line feeds.
Would you know how this could be done.
How are you trying to copy the data? Are you opening it in an edit window and trying to do a copy paste or are you just doing an update?
I am trying to copy the data using an update statement.
set field1 = field2
where field2 is not null;
To check the output in the SQL server, have also set ‘Retain CR/LF on copy or save’ under query options.
when the data is pulled into a java application the copied field(field1) it loses formatting, though the original field (field2) has the formatted text. In the java application have a simple select statement.
Basically, the field1 is truncating NCHAR(10) and NCHAR(13) in the text, thus the text is unformatted.
This is all i was am doing and able to understand.
The ‘Retain CR/LF on copy or save’ only effects copy and paste operations. Not update statements, or how you are transferring the data to your application. If I had to guess it’s how you are displaying the data that’s the issue not the data copy. Or it could be the method you are using to get the data.