PostgreSQL WHERE IN LIKE query

Users of MySQL or Oracle may find it a bit different, but in PostGreSQL, to filter data using LIKE clause, one should use something like -

select * from table-name where column-name::text like '%whatever-to-be-searched%'

Actually using something IN (<value list>) is similar to something = any(array[<value list>]) in the PostgreSQL:

postgres=# explain select 1 where 'a' in ('a','b','c');
                        QUERY PLAN                        
----------------------------------------------------------
 Result  (cost=0.00..0.01 rows=1 width=0)
   One-Time Filter: ('a'::text = ANY ('{a,b,c}'::text[]))
(2 rows)

Fortunately we can use like or even ilike instead of =:

select 1 where 'aa' ilike any(array['%A%','%B%','%C%']);
 ?column? 
----------
        1
(1 row)

So in your case it could be

... state LIKE ANY(ARRAY['%idle%', '%disabled%'])

And the additional advantage: it can be passed as a parameter from the client application.


x IN (a, b) can be consisidered shorthand for x = ANY (ARRAY[a,b]). Similarly, x IN (SELECT ...) and x = ANY (SELECT ...).

The = can actually be replaced by any binary operator. Thus, you can use:

SELECT ... WHERE x LIKE ANY (SELECT ...)

Use SIMILAR TO instead of LIKE

AND state SIMILAR TO '%(idle|disabled)%'

https://www.postgresql.org/docs/9.0/static/functions-matching.html