How to escape wildcard characters in “like” clause

2019-02-16 22:26发布

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?

1条回答
一夜七次
2楼-- · 2019-02-16 23:07

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.

查看更多
登录 后发表回答