开发者

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

开发者 https://www.devze.com 2023-03-29 00:09 出处:网络
I have a table T1 with a column details, data column has follow开发者_运维问答ing sample data :

I have a table T1 with a column details, data column has follow开发者_运维问答ing 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 ?


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


Try using '\\%\\%\\' - using a backslash to escape the backslash.

0

精彩评论

暂无评论...
验证码 换一张
取 消