Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query for a carriage return in a string and ultimately removing carriage return

SQL query for a carriage return in a string and ultimately removing carriage return

I have some data in a table and there are some carriage returns in places where I don't want them. I am trying to write a query to get all of the strings that contain carriage returns.

I tried this

select * from Parameters where Name LIKE '%"\n" %' 

Also

select * from Parameters where Name LIKE '\r' 

'

Both are valid SQL but are not returning what I am looking for. Do I need to use the Like command or a different command? How do I get the carriage return into the query?

The carriage return is not necessarily at the end of the line either (may be in the middle).

like image 384
Maestro1024 Avatar asked Aug 26 '09 20:08

Maestro1024


People also ask

How do I remove a carriage return in SQL query?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.

How do I query a carriage return in SQL?

In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

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.


2 Answers

this will be slow, but if it is a one time thing, try...

select * from parameters where name like '%'+char(13)+'%' or name like '%'+char(10)+'%' 

Note that the ANSI SQL string concatenation operator is "||", so it may need to be:

select * from parameters where name like '%' || char(13) || '%' or name like '%' || char(10) || '%' 
like image 98
KM. Avatar answered Sep 21 '22 01:09

KM.


The main question was to remove the CR/LF. Using the replace and char functions works for me:

Select replace(replace(Name,char(10),''),char(13),'') 

For Postgres or Oracle SQL, use the CHR function instead:

       replace(replace(Name,CHR(10),''),CHR(13),'') 
like image 41
Grant Dever Avatar answered Sep 19 '22 01:09

Grant Dever