Print

Include a carriage return in a column heading

Applies To:
What?
A really quick note on how to insert a carriage return or new line into the column name/alias (the header). It might seem trivial but these little aesthetic changes done at the database level can save some time.

Why?
I have an Excel report which dynamically gets its content from a data source located on a database on the other side of the world. I want the header in the column "Academic Week" to break across two lines so that the column doesn't expand to the width of "Academic Week" and instead expands to the width of the word "Academic".

What I have:
copyraw
Academic Week    Monday      Tuesday     Wednesday   Thursday    Friday
---------------- ----------- ----------- ----------- ----------- -----------
1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014
2                21-Jul-2014 22-Jul-2014 23-Jul-2014 24-Jul-2014 25-Jul-2014
...
  1.  Academic Week    Monday      Tuesday     Wednesday   Thursday    Friday 
  2.  ---------------- ----------- ----------- ----------- ----------- ----------- 
  3.  1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014 
  4.  2                21-Jul-2014 22-Jul-2014 23-Jul-2014 24-Jul-2014 25-Jul-2014 
  5.  ... 
What I want:
copyraw
Academic 
Week      Monday      Tuesday     Wednesday   Thursday    Friday
--------- ----------- ----------- ----------- ----------- -----------
1         14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014
2         21-Jul-2014 22-Jul-2014 23-Jul-2014 24-Jul-2014 25-Jul-2014
...
  1.  Academic 
  2.  Week      Monday      Tuesday     Wednesday   Thursday    Friday 
  3.  --------- ----------- ----------- ----------- ----------- ----------- 
  4.  1         14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014 
  5.  2         21-Jul-2014 22-Jul-2014 23-Jul-2014 24-Jul-2014 25-Jul-2014 
  6.  ... 


How?
To do this in a select query resultset, you insert the special character references "CHAR(10)" [line feed] and "CHAR(13)" [carriage return] but to do this in the name of the column heading, the answer is a much simpler one, in your SQL statement, simply place your cursor where you want the carriage return and press Return/Enter. This has to be a label to the name of the column enclosed between square brackets or double-quotes:

Before:
copyraw
SELECT			
	calendar.WeekNumber AS [Academic Week],		
	calendar.Monday,
	calendar.Tuesday,
	calendar.Wednesday,
	calendar.Thursday,
	calendar.Friday
FROM			
	calendar

-- yields as above:

-- Academic Week    Monday      Tuesday     Wednesday   Thursday    Friday
-- ---------------- ----------- ----------- ----------- ----------- -----------
-- 1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014
  1.  SELECT 
  2.      calendar.WeekNumber AS [Academic Week], 
  3.      calendar.Monday, 
  4.      calendar.Tuesday, 
  5.      calendar.Wednesday, 
  6.      calendar.Thursday, 
  7.      calendar.Friday 
  8.  FROM 
  9.      calendar 
  10.   
  11.  -- yields as above: 
  12.   
  13.  -- Academic Week    Monday      Tuesday     Wednesday   Thursday    Friday 
  14.  -- ---------------- ----------- ----------- ----------- ----------- ----------- 
  15.  -- 1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014 
After:
copyraw
SELECT			
	calendar.WeekNumber AS [Academic 
	Week],		
	calendar.Monday,
	calendar.Tuesday,
	calendar.Wednesday,
	calendar.Thursday,
	calendar.Friday
FROM			
	calendar

-- yields:

-- Academic 
-- Week             Monday      Tuesday     Wednesday   Thursday    Friday
-- ---------------- ----------- ----------- ----------- ----------- -----------
-- 1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014
  1.  SELECT 
  2.      calendar.WeekNumber AS [Academic 
  3.      Week], 
  4.      calendar.Monday, 
  5.      calendar.Tuesday, 
  6.      calendar.Wednesday, 
  7.      calendar.Thursday, 
  8.      calendar.Friday 
  9.  FROM 
  10.      calendar 
  11.   
  12.  -- yields: 
  13.   
  14.  -- Academic 
  15.  -- Week             Monday      Tuesday     Wednesday   Thursday    Friday 
  16.  -- ---------------- ----------- ----------- ----------- ----------- ----------- 
  17.  -- 1                14-Jul-2014 15-Jul-2014 16-Jul-2014 17-Jul-2014 18-Jul-2014 

Last Step:
Format the cells in Excel containing the header under "Alignment" to "Wrap Text". The difference the above does, is that when you refresh the data, it will retain the carriage return in the column headings.

Additional:

Other Search(es)
Category: SQL Server :: Article: 568