Copying formatted text out of a grid column.


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