How can I remove unwanted carriage-returns and line-feeds from a SQL table?

Neil Pike

July 27, 1999

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Here is a script courtesy of fellow MVP, Bob Pfeiff. Note, it will only get rid of one CRLF per column, so you may want to stick the update in loop and keep doing it until you get a zero rowcount.

--example on pubs database in SQL 7

--insert a row with a carriage return and line feed in it

insert authors (au_id, au_lname, au_fname, phone, contract)
values ( '111-34-3420', char(10) + char(13), 'john', '222 333-8899', 1)

--find the row with the cr/lf in it

select
*
from
authors
where
au_lname = char(10) + char(13)

--update the column with the cr/lf with an empty string

update
authors
set
au_lname = stuff(au_lname, patindex(char(10)+ char(13), au_lname), 2, '')
where 
patindex(char(10) + char(13), au_lname) > 0

--look for the row with the cr/lf in it (should be gone)

select
*
from
authors
where
au_lname = char(10) + char(13)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like