SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

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?


My name is Kenneth Fisher and I am Senior DBA for a large (multi-national) insurance company. I have been working with databases for over 20 years starting with Clarion and Foxpro. I’ve been working with SQL Server for 12 years but have only really started “studying” the subject for the last 3. I don’t have any real "specialities" but I enjoy trouble shooting and teaching. Thus far I’ve earned by MCITP Database Administrator 2008, MCTS Database Administrator 2005, and MCTS Database Developer 2008. I’m currently studying for my MCITP Database Developer 2008 and should start in on the 2012 exams next year. My blog is at www.sqlstudies.com.


Leave a comment on the original post [sqlstudies.com, opens in a new window]

Loading comments...