Replace newline in ntext

FoxyReign picture FoxyReign · Nov 16, 2015 · Viewed 7.9k times · Source

One of the columns uses ntext. I want to replace the newlines and carriage returns in to space but I can only do it one at a time. Is there a way I could use CHAR(10) and CHAR(13) at the same time using the script below on SQL Server 2012?

REPLACE(CAST(Description as NVARCHAR(MAX)), CHAR(10), ' ') AS Description

Answer

Alan picture Alan · Nov 16, 2015

To replace CRLF instances, you could use:

REPLACE(CAST(Description as NVARCHAR(MAX)), CHAR(13)+CHAR(10), ' ') AS Description

To individually replace all CRs and LFs, you could use:

REPLACE(REPLACE(CAST(Description as NVARCHAR(MAX)), CHAR(10), ' '), CHAR(13), ' ') AS Description