Home > OS >  Copy and Paste T-SQL query result into SSMS Text Editor with Column Align
Copy and Paste T-SQL query result into SSMS Text Editor with Column Align

Time:10-13

When I copy and paste the query results into the text editor the columns are miss-aligned and hard to read. Is there a way to copy and paste into the text editor with column align like when you copy and paste the query result into Excel or Sheets it keeps the columns aligned? Is there a setting, extension or more advanced text query editor that will keep the columns aligned? Is there a way to add an extension using the Excel or Sheets paste function that will keep the columns aligned?

Example

INSERT INTO ##TestTable(Name, UpdateByApp1Date, UpdateByApp2Date, UpdateByApp3Date )
VALUES('ABC', '2015-08-05','2015-08-04', '2015-08-06'),
      ('NewCopmany', '2014-07-05','2012-12-09', '2015-08-14'),
      ('MyCompany', '2015-03-05','2015-01-14', '2015-07-26')
      
SELECT * FROM ##TestTable

--ID    Name    UpdateByApp1Date    UpdateByApp2Date    UpdateByApp3Date
--1 ABC 2015-08-05 00:00:00.000 2015-08-04 00:00:00.000 2015-08-06 00:00:00.000
--2 NewCopmany  2014-07-05 00:00:00.000 2012-12-09 00:00:00.000 2015-08-14 00:00:00.000
--3 MyCompany   2015-03-05 00:00:00.000 2015-01-14 00:00:00.000 2015-07-26 00:00:00.000

I want to see the results copy paste into the text editor with columns aligned

--ID    Name        UpdateByApp1Date        UpdateByApp2Date        UpdateByApp3Date
--1     ABC         2015-08-05 00:00:00.000 2015-08-04 00:00:00.000 2015-08-06 00:00:00.000
--2     NewCopmany  2014-07-05 00:00:00.000 2012-12-09 00:00:00.000 2015-08-14 00:00:00.000
--3     MyCompany   2015-03-05 00:00:00.000 2015-01-14 00:00:00.000 2015-07-26 00:00:00.000

CodePudding user response:

When you copy and paste the standard format, it puts tabs between each column - a relatively standard process for pasting into tables (e.g., Excel). SQL Server Management Studio, by default, outputs its results that copy to this tab-delimited format.

However, you can change the type of output which changes the way it copies. If you change it to output to 'Results to text', it formats it as a text table.

To change the output/results mode, you can

  • Change it in preferences (Tools -> Options -> Query results -> SQL Server)
  • In the main working area, before you run the query, you can right-click on/around the SQL statement area and select 'Results To -> Text'
  • Before running the query, hit CTRL-T (hit CTRL-D to revert to the normal output to grid).

Note that when you paste the text from the output, to keep spacing lined up properly, you will need to use a non-proportional font (like Courier New, or Lucida Console). These are the default in programs like Notepad, but not in many word processors etc.

  • Related