Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert "\n" to actual newline in SQL Server

I have a bunch of varchar(255) and varchar(max) fields in a table in MS SQL Server.

These are generally formatted messages (email and other). Most of the fields have the actual characters "\n", but actually need a newline character. I don't need to worry about new data going forward, but don't know how to fix the stuff that's currently in the DB.

I'm mostly a programmer, not a SQL/DB Guy, so any pointers on how to approach fixing this, or resources to get me started would be appreciated.

like image 520
Kylar Avatar asked Jan 12 '10 02:01

Kylar


People also ask

How do you change a line break in SQL Server?

Remove and Replace Carriage Returns and Line Breaks in SQL Using SQL to remove a line feed or carriage return means using the CHAR function. A line feed is CHAR(10); a carriage return is CHAR(13).

What does \n mean in SQL?

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

How do I create a new line in SQL query?

-- Using both \r\n SELECT 'First line. \r\nSecond Line. ' AS 'New Line'; -- Using both \n SELECT 'First line.


2 Answers

this should do the trick

UPDATE
    <tablename> 
SET
    <fieldname> = replace(<fieldname>,'\n',char(13)+char(10)),
    <otherfieldname> = replace(< otherfieldname >,'\n',char(13)+char(10))
like image 178
Gabriele Petrioli Avatar answered Sep 20 '22 17:09

Gabriele Petrioli


This:

print replace('Line 1\nLine 2','\n',char(13))

will produce:

Line 1
Line 2
like image 21
JBrooks Avatar answered Sep 21 '22 17:09

JBrooks