Our application supports wildcard searches of the data, and the data occasionally includes LIKE "special" characters, such as '%', '_', and '\'. One thing we learned recently is that escaping a backslash, which is the LIKE default "escape character" in a LIKE query is particularly troublesome.
Here's some examples. Consider the below simple table:
> insert into foo (a) values ('\\');
> insert into foo (a) values ('\\%');
> select * from foo \G
a: '\'
a: '\%'
Now, do a LIKE search of a single backslash character without a wildcard. To get it searched on, you have to add THREE backslashes to the string, and need a total of four backslashes in the string:
> select * from foo where a like '\\\\' \G
a: '\'
To do a LIKE search of a single backslash and a single "escaped" percent sign, you need six backslashes: four to embed one in the data, and two to escape the percent sign:
> select * from foo where a like '\\\\\\%' \G
a: '\%'
Note that if you use simple equality, you need two backslashes to search on a single backslash.
No comments:
Post a Comment