What is the science behind the char(13) resulting in a new line in SQL server?Always wondered how it works.

2

Best Answer


Too long to comment, just to elaborate a little bit on the comments...

If you look at an ASCII table, you will see the decimal and character values for each. You can check / view these in SQL using the ASCII and CHAR functions.

select ASCII_Decimal_Value = ascii('!'),ASCII_Character = char(33)

The snippet above shows the decimal and character values for an exclamation point. Knowing this, you should understand why 13 and 10 is used for carriage return and new line, respectively, and how to use it. For example... if you run the code below in SSMS with the output as Results to Text, you can see the carriage return.

select 'This will be on the first line' + char(13) + 'and this will be on the second'

13 is ASCII the value of carriage return character.