Escape From SQL
The other day, one of my coworkers was presented a question from our development team:
How can I use SQL to look for a "%" in a column?
Well, the first guess is to use something like:
SELECT * FROM MYLIB/MYFILE WHERE MYCOL LIKE ‘%%%’
or
SELECT * FROM MYLIB/MYFILE WHERE MYCOL LIKE ‘%\%%’
Well, unfortunately, neither of those statements would work. However, DB2/OS400 provides a very cool way of escaping a special character. It is the ESCAPE keyword. This keyword allows you to specify the character that you would like to use as an escape character. So, this would be the correct syntax:
SELECT * FROM MYLIB/MYFILE WHERE MYCOL LIKE ‘%+%%’ ESCAPE ‘+’
I think that this is a very cool feature. I just wish that the ANSI/ISO standards used it. Then, it could be used in other databases.
