How to insert a line break in a SQL Server VARCHAR/NVARCHAR string
char(13)
is CR
. For DOS-/Windows-style CRLF
linebreaks, you want char(13)+char(10)
, like:
'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
I found the answer here: http://blog.sqlauthority.com/2007/08/22/sql-server-t-sql-script-to-insert-carriage-return-and-new-line-feed-in-code/
You just concatenate the string and insert a CHAR(13)
where you want your line break.
Example:
DECLARE @text NVARCHAR(100)
SET @text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT @text
This prints out the following:
This is line 1.
This is line 2.
Another way to do this is as such:
INSERT CRLF SELECT 'fox
jumped'
That is, simply inserting a line break in your query while writing it will add the like break to the database. This works in SQL server Management studio and Query Analyzer. I believe this will also work in C# if you use the @ sign on strings.
string str = @"INSERT CRLF SELECT 'fox
jumped'"