Case insensitive LIKE in DB2
-
23-02-2021 - |
Question
Is there a way to do a LIKE
operation in DB2 against a varchar column in a case insensitive way, without changing the underlying column definition?
Or is there a workaround like SELECT * FROM mytable WHERE mycolumn LIKE '%string%' OR mycolumn LIKE '%STRING%'
that would work?
Solution
No. Assuming Db2 for LUW, you can choose a case-insensitive collating table, but only at the database creation time.
You'll have to standardise the case for comparison, with obvious performance implications:
SELECT * FROM mytable WHERE LCASE(mycolumn) LIKE '%string%'
OTHER TIPS
Try
SELECT * FROM mytable WHERE REGEXP_LIKE(mycolumn,'string',1,'i')
Will work if your Db2 version supports regular expression functions https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0061494.html and if you escape any regex meta-characters in your string