5

I have read many articles explaining what CHAR(10) and CHAR(13) actually are.

I have no problem with CHAR(10), it is simply a line feed or a new line.

I do not understand how CHAR(13) behaves in SQL Server.

See below simple code:

Case 1 - Simple new line
Case 2 - Again, a new line. But why?
Case 3 - 2 new lines. Assuming char(10) and char(13) are returning a new line each (Case 2).
Case 4 - Why 1 new line only?

PRINT '----------Case 1----------'
DECLARE @str1 nVarchar(MAX) = 'Hello' + CHAR(10) + 'World'
PRINT @str1
PRINT '----------Case 2----------'
DECLARE @str2 nVarchar(MAX) = 'Hello' + CHAR(13) + 'World'
PRINT @str2
PRINT '----------Case 3----------'
DECLARE @str3 nVarchar(MAX) = 'Hello' + CHAR(10) + CHAR(13) + 'World'
PRINT @str3
PRINT '----------Case 4----------'
DECLARE @str4 nVarchar(MAX) = 'Hello' + CHAR(13) + CHAR(10) + 'World'
PRINT @str4
PRINT '----------END----------'

See this screenshot:

5
  • 8
    CR (13) + LF (10) combine to create 1 total carriage return. If you do it in the opposite order, the LF forces the CR to be on a new line, producing 2 carriage returns. It's why in Visual Basic, for example, they call it vbCrLf. Always put the carriage return first. If you're just printing stuff in SSMS, though, you only need one or the other. Commented Aug 16, 2023 at 13:33
  • 2
    If we go to what the root of a CR control key means, move the cursor to the start of the line, it actually shouldn't create a new line. Some (many) tools, however, will infer a CR without a following LF to mean to start a new line, even though technically it doesn't mean that at all. Commented Aug 16, 2023 at 13:45
  • To be honest, this is not a SQL specific thing, it's actually a holdover from some very old machines that needed separate characters. Either CR or LF or CR/LF will make a newline on most modern machines. Pick one and stick to it. Commented Aug 16, 2023 at 18:44
  • Both of these are control characters - that were originally created to send commands to a printer. The carriage return instructed the printer to move the carriage (print head) back - the line feed advances the paper one line. For files - the end of line is determined by the default for the platform. Windows default is CR/LF - Unix default is LF and MAC is CR (or was - may have changed). Either way, when creating a file in Windows the end of line will be denoted by a combination of CR/LF . Commented Aug 16, 2023 at 21:16
  • 2
    This is mostly covered by What are carriage return, linefeed, and form feed?. The key point is that the combination of the two characters is how line breaks are traditionally represented in DOS/Windows software. Commented Jul 8 at 12:58

1 Answer 1

-1

I will just give you a line that for some is counter intuitive

PRINT 'Hello
World!'

This is one statement in Sql Server, and works for everything and most likely also if ported to Unix. it works for update statements and everything else. same with

PRINT 'Hello' + '
' + 'World'

so you avoid the CHAR(10/13) in your SQL.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.