Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an SQL result contains a newline character?

I have a varchar column that contains the string lol\ncats, however, in SQL Management Studio it shows up as lol cats.

How can I check if the \n is there or not?

like image 780
NibblyPig Avatar asked Oct 06 '10 12:10

NibblyPig


People also ask

How do you check if a string contains a character in SQL?

We can use the CHARINDEX() function to check whether a String contains a Substring in it. Name of this function is little confusing as name sounds something to do with character, but it basically returns the starting position of matched Substring in the main String.

How do I remove a line break character in SQL?

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.


2 Answers

SELECT * FROM your_table WHERE your_column LIKE '%' + CHAR(10) + '%' 

Or...

SELECT * FROM your_table WHERE CHARINDEX(CHAR(10), your_column) > 0 
like image 72
LukeH Avatar answered Oct 05 '22 02:10

LukeH


Use char(13) for '\r' and char(10) for '\n'

SELECT * FROM your_table WHERE your_column LIKE '%' + CHAR(10) + '%' 

or

SELECT * FROM your_table WHERE your_column LIKE '%' + CHAR(13) + CHAR(10) + '%' 
like image 40
Sachin Shanbhag Avatar answered Oct 05 '22 02:10

Sachin Shanbhag