Wednesday, May 9, 2012

Line Break In SQL Sever...


I need to format a string, like separate lines.  While displaying that string i need split into two lines. To do that follow the bellow steps. we have two different options to get new line.
What is difference between Line Feed (\n) and Carriage Return (\r)?
   1. Line Feed – LF – \n – 0x0a – 10 (decimal)
   2. Carriage Return – CR – \r – 0x0D – 13 (decimal)
Different operating systems have a different way of understanding new line. Mac only understands ‘\r’ as new line, while Unix and Linux understand ‘\n’ as new line character. Our favorite OS windows needs both the characters together to interpret as new line, which is ‘\r\n’. This is the reason why a file created in one OS does not open properly in another OS and makes it messy.

we can create a new line in SQL Server. It is a very simple script yet very useful when we have to do run print something or generate scripts. Here is two examples below that are very easy to understand. In the first example, there are no new line chars inserted and for the same, everything is displayed in a single line. However, in the second example, new line char is inserted and the lines are separated with a new line.
Example 1: No new line feed char
DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
PRINT ('SELECT FirstLine AS FL SELECT SecondLine AS SL' )
GO
Example 2: With new line feed char

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
PRINT ('SELECT FirstLine AS FL ' +@NewLineChar +'SELECT SecondLine AS SL' )
GO

No comments:

Post a Comment