Pattern Matching

Question:
How can I use SQL to specify a query like the example below:

    select * from mytable where column like ‘a[bc]’
This should match both “ab” and “ac” but not “ad.”

Answer:
SQL does not have strong pattern matching facilities. For the example you’ve provided, you’ll have to construct the query using the IN predicate:

    SELECT * FROM MyTable         WHERE col IN (ab, ac);
If your list is extensive, you might want to use a subquery:
    SELECT * FROM MyTable         WHERE col IN             (SELECT value FROM ValueTable);

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: