86

Is there any shorter way to look for multiple matches:

 SELECT * 
 from table 
 WHERE column LIKE "AAA%" 
    OR column LIKE "BBB%" 
    OR column LIKE "CCC%"

This questions applies to PostgreSQL 9.1, but if there is a generic solution it would be even better.

3
  • It reads well, works well and is easy to understand. Why swap that for alternatives like the popular answer and get into knots when you need to find pipes (|) or brackets in actual data? Commented Oct 18, 2012 at 16:07
  • @RichardTheKiwi Because is it not as easy to dynamically build. See my answer on how to avoid dealing with a regex. Commented Oct 19, 2012 at 12:25
  • 1
    @Clo It's still code that's not ISO standard and harder to understand by comparison. Except possibly any (values('AAA%'), ('BBB%'), ('CCC%') That one looks good. Commented Oct 19, 2012 at 12:30

7 Answers 7

96

Perhaps using SIMILAR TO would work ?

SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%';
Sign up to request clarification or add additional context in comments.

4 Comments

+1 SIMILAR TO would work, but for the example in the question it should be SIMILAR TO 'AAA%|(BBB|CCC)%' or SIMILAR TO '(AA%|BBB%|CCC%'
Similar to was added to Postgres 7.3, so Netezza systems and the like may not allow its use.
Very elegant solution
Brilliant! Never knew or heard about this. Good one.
53

Use LIKE ANY(ARRAY['AAA%', 'BBB%', 'CCC%']) as per this cool trick @maniek showed earlier today.

2 Comments

You just missed the syntax. The list must be array or set.
I believe ANY was added in Postgres 7.4.
35

Using array or set comparisons:

create table t (str text);
insert into t values ('AAA'), ('BBB'), ('DDD999YYY'), ('DDD099YYY');

select str from t
where str like any ('{"AAA%", "BBB%", "CCC%"}');

select str from t
where str like any (values('AAA%'), ('BBB%'), ('CCC%'));

It is also possible to do an AND which would not be easy with a regex if it were to match any order:

select str from t
where str like all ('{"%999%", "DDD%"}');

select str from t
where str like all (values('%999%'), ('DDD%'));

1 Comment

I believe ANY and ALL were added in Postgres 7.4.
15

You can use regular expression operator (~), separated by (|) as described in Pattern Matching

select column_a from table where column_a ~* 'aaa|bbb|ccc'

Comments

6

Following query helped me. Instead of using LIKE, you can use ~*.

select id, name from hosts where name ~* 'julia|lena|jack';

Comments

1

if you want use not like to skip values use this query example:

select * from table 
where not field  like any(array['%aaa%','%bbb%','%ccc%','%ddd%'])

Comments

-7

You might be able to use IN, if you don't actually need wildcards.

SELECT * from table WHERE column IN ('AAA', 'BBB', 'CCC')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.