Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do apply SQL like on "\detail1\detail2\" (Escaping '\')?

I have a table T1 with a column details, data column has following sample data :

select details from T1;

\this is detail 1\this is detail2\
\this is detail 1\this is detail2\
:this is detail 1:this is detail2:
:this is detail 1:this is detail2:
\this is detail 1\this is detail2\

I want to fetch only those details which are separated by \.

For that I wrote the following query :

select details from T1 where details like '\%\%\';

But its not responding as expected, since its taking \ as escape corrector.

So, its showing error of incomplete single inverted(') !

So, how to do this ?

like image 593
Yugal Jindle Avatar asked Aug 19 '11 08:08

Yugal Jindle


People also ask

How do I escape a SQL like statement?

The ESCAPE clause is supported in the LIKE operator to indicate the escape character. Escape characters are used in the pattern string to indicate that any wildcard character that occurs after the escape character in the pattern string should be treated as a regular character.

What does escaping mean in SQL?

Escape sequences are used within an SQL statement to tell the driver that the escaped part of the SQL string should be handled differently. When the JDBC driver processes the escaped part of an SQL string, it translates that part of the string into SQL code that SQL Server understands.


2 Answers

Try this, you need to escape backslashes twice in LIKE statement.

select details from T1 where details like '\\\\%\\\\%\\\\'

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.

like image 53
Juha Syrjälä Avatar answered Sep 30 '22 12:09

Juha Syrjälä


MySql has escape character \\ for backslash (same as in C-like languages):

http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

like image 37
Mike Avatar answered Sep 30 '22 13:09

Mike