Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly insert newline in nvarchar

I need to make a script that takes some rich text (html text) and transform it to regular text. What I want to do is replace all <br> tags with newline. I tried to do this with replace function:

set @rich_text_to_modify = replace(@rich_text_to_modify,'<br>', CHAR(13)+CHAR(10))

The
tags get removed but newlines are not inserted. Any idea what I am doing wrong?

like image 238
OjamaYellow Avatar asked Nov 02 '18 09:11

OjamaYellow


People also ask

How do I insert a line break in varchar nvarchar string in SQL Server?

You just concatenate the string and insert a CHAR(13) where you want your line break. This prints out the following: This is line 1. This is line 2.

What is the N in nvarchar N )?

First of all, What is nvarchar(n)? Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB).

What does \n mean in SQL?

Background. The "N" prefix stands for National Language in the SQL-92 standard, and is used for representing Unicode characters.


1 Answers

The problem is your setting on SSMS, not that the data doesn't have a line break.

Go to: Tools -> Options -> Query Results -> SQL Server -> Results to Grid -> Retain CR/LF on copy or Save and make sure the option is ticked.

like image 123
Larnu Avatar answered Nov 15 '22 17:11

Larnu