Break text to new line sql server database
In SQL Server, you can use the CHAR(13) + CHAR(10)
or CHAR(10)
to break text to a new line. Here are some examples:
Using CHAR(13) + CHAR(10)
SELECT 'Hello ' + CHAR(13) + CHAR(10) + 'World!' AS NewLineText
This will output:
Hello
World!
Using CHAR(10)
SELECT 'Hello ' + CHAR(10) + 'World!' AS NewLineText
This will also output:
Hello
World!
Using REPLACE() function
SELECT REPLACE('Hello World!', ' ', CHAR(13) + CHAR(10)) AS NewLineText
This will also output:
Hello
World!
Using STUFF() function
SELECT STUFF('Hello World!', 7, 0, CHAR(13) + CHAR(10)) AS NewLineText
This will also output:
Hello
World!
Note that CHAR(13)
is the carriage return character and CHAR(10)
is the line feed character. When you combine them, you get a new line.
You can also use these methods to break text to a new line in a stored procedure, function, or trigger. For example:
CREATE PROCEDURE BreakTextToNewLine
@text nvarchar(max)
AS
BEGIN
SELECT @text + CHAR(13) + CHAR(10) AS NewLineText
END
You can then call the stored procedure and pass in the text you want to break to a new line:
EXEC BreakTextToNewLine 'Hello World!'
This will output:
Hello
World!