Home > Net >  Is it possible to adjust an array in PostgreSQL to fit with an IN operator?
Is it possible to adjust an array in PostgreSQL to fit with an IN operator?

Time:01-24

Something like:

SELECT * FROM table WHERE something IN ('{"val1","val2"}'::text[]);

I tried it with array_to_string().

SELECT * FROM table WHERE something IN (array_to_string('{"val1","val2"}'::text[]));

But I guess that makes it to this:

SELECT * FROM table WHERE something IN ('val1,val2'); --one single string

I guess the single values must also be surrounded with apostrophes.

Is that possible somehow, or can it be solved in a completely different way?

CodePudding user response:

Use the ANY operator:

SELECT * 
FROM table 
WHERE something = ANY ('{"val1","val2"}'::text[]);

CodePudding user response:

You are looking for ANY:

SELECT * 
FROM example
WHERE something = ANY('{"val1","val2"}'::text[]);

But if you insist on using IN, you can achieve the same with UNNEST:

SELECT * 
FROM example
WHERE something IN (SELECT UNNEST('{"val1","val2"}'::text[]));

(online demo)

  • Related