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 ?
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.
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.
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.
MySql has escape character \\ for backslash (same as in C-like languages):
http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With