Tags

,


The terminology originates in the typewriter’s operation. Carriage return returns the carriage with the typing paper to the left margin. Line Feed rotates the carriage upward to advance the typing paper to the next line, the paper stays the current position.

Now before we go ahead with the discussion, lets first discuss “Why do we have both Carriage Return and Line Feed?”

Well, the answer is because different operating systems have different way of understanding new line. Windows uses carriage return line feed ‘\r\n’, UNIX and newer Mac use line feed ‘\n’, and older Mac use carriage return ‘\r’. This is the reason when transferring files from one platform from another, you can experience that ENTERs are not translated in a way that you would expect making the file look messy.

Now lets see how we can create new line in SQL Server. I will illustrate this with the help of two examples. In the first example, there are no new line characters, everything is displayed in the single line. However, in the second example new line character is inserted and the lines are separated with new line.

First Example: No new line feed character is inserted in the code.

DECLARE @CarriageReturn_LineFeed VARCHAR(100)
SET @CarriageReturn_LineFeed=‘Line One.[\r\n];Line Two.[\r\n];Line Three.[\r\n];’
Print @CarriageReturn_LineFeed

Second Example: With New line feed character.

DECLARE @CarriageReturn_LineFeed VARCHAR(100)
SET @CarriageReturn_LineFeed=‘Line One.[\r\n];Line Two.[\r\n];Line Three.[\r\n];’
SET @CarriageReturn_LineFeed=REPLACE(@CarriageReturn_LineFeed,'[\r\n];’,CHAR(13)+CHAR(10))
Print @CarriageReturn_LineFeed

I hope the above example cleared all the doubts you had. Thank you and keep learning!!

Advertisements