Jump to Real's How-to Main page

Escape special character in a LIKE clause

Your JDBC driver may support the {escape 'escape character'} syntax for using LIKE clause wildcards as literals. The escape sequence must be at the end of the SQL statement.

Searching for "one_word"

st = con.createStatement();
rs = st.executeQuery
 ("SELECT value FROM vendors WHERE value LIKE 'one/_word' {escape '/'}");

Searching for strings ending with "one%word"

st = con.createStatement();
rs = st.executeQuery
 ("SELECT value FROM vendors WHERE value LIKE '%one/%word' {escape '/'} ");

Find all rows in which a begins with the character "%"

st = con.createStatement();
rs = st.executeQuery
 ("SELECT value FROM vendors WHERE value LIKE '$%%' {escape '$'}");

Find all rows in which a ends with the character "_"

st = con.createStatement();
rs = st.executeQuery
 ("SELECT value FROM vendors WHERE a LIKE '%=_' {escape '='}");

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2007
[ home ]