Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

Use the ANY operator:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading