Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - How to handle query search with special characters /(forward slash) and \(backslash)

Tags:

mysql

plsql

I have a table where a column allows special characters like '/' (forward slash) and '' (back slash).

Now when I try to search such records from table, I am unable to get those.

For example: abc\def or abc/def

I am generating a search query like:

select * from table1_1 where column10 like '%abc\def%'

It is returning 0 rows, but actually there is 1 record existing that should be returned. How do I write the query in this case?

like image 746
JAVAC Avatar asked May 10 '13 20:05

JAVAC


People also ask

How do you handle special characters in SQL query?

How do you handle special characters in SQL query? Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence.


1 Answers

In MySQL, this works:

select * from Table1
where column10 like '%abc\\\\def%'

FIDDLE

Backslash is an escape prefix for both strings and LIKE patterns. So you need to double it once for LIKE, and again for string literal syntax.

like image 154
Barmar Avatar answered Sep 27 '22 21:09

Barmar