Evaluating like on string that contains a percent sign

(This should give someone a quick Correct Answer)

If you want to find things that start with "%Inh", how does one fashion the LIKE statement (no cheating with RLIKE) so that the percent sign is taken literally (i.e. how do you escape the percent sign)? I tried \\%, CHR(37), nested ' and " in various ways, can't quite get it.

Answers 3
SB
SB 2014-12-09

You can enclose '%', '_', or '[' within square brackets: '[ ]'. 

']' can be on its own

So, you can do any of these:

prop like '[%]Inh';

prop like '[_]Inh';

prop like '[[]Inh';

prop like ']Inh';

Underneath the surface, both "like" and "rlike" use the same pattern matching engine, so there isn't a performance benefit of one versus the other.  "like" patterns are internally converted to equivalent "rlike" regular expressions.

Steve

LH
LH 2014-12-10

Thanks Steve.

It's news to me that an underscore at the beginning of a property name has to be escaped. Good to know.

AA
AA 2014-12-30

Lee,

An underscore is a single character wildcard when used with LIKE.  That's why Steve showed how to escape it.

Malcolm