Copying formatted text out of a grid column.

3

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?

3 thoughts on “Copying formatted text out of a grid column.

  1. […] Kenneth Fisher shows us how to retain CR/LF values when copying and pasting from SQL Server Manageme…: […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2,469 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: