Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape wildcard characters in "like" clause?

How can I escape the wildcard characters in a like clause?

E.g.:

select foo from Foo as foo where foo.bar like '%' || :filter ||'%'
query.setParameter("filter", "%");
query.list(); 
// I'd expect to get the foo's containing the '%' in bar, not all of them!

Any ideas?

like image 479
Pablo Cabrera Avatar asked Jun 09 '10 13:06

Pablo Cabrera


People also ask

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What are the two wildcard characters used with the like clause?

There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one, or multiple characters. The underscore represents a single number or character. These symbols can be used in combinations.


1 Answers

In Hibernate 3 you can use the escape parameter to specify the escape char:

select foo from Foo as foo where foo.bar like '!%' escape '!'

I think that should work, although I have never tried it in practice.

like image 97
Dean Povey Avatar answered Nov 26 '22 14:11

Dean Povey