PostgreSQL: How to convert comma separated string to work with IN operator

Viewed 3018

I was trying to make this query to work

select *
from DUALS
where num in ('1,2,3')

string '1,2,3' creating on the fly, so i cant replace it as list of integers in code.

So I end with this:

select *
from DUALS
where num in (select unnest (string_to_array('1,2', ',')::integer[]))

It works, but I'm wondering if it might be simplified. Can't use 'Any' operator.

1 Answers

You can just use this code:

select *
from DUALS
where num = any('{1,2,3}'::int[])
Related