7


I am trying to write a query to check if an element is within an array of Strings.

Here is my simple select query along with the output

select languages from person limit 3;
{CSS,HTML,Java,JavaScript,Python}
{JavaScript,Python,TensorFlow}
{C++,Python}

How do I write a query to find all people who have "Java" as a listed language they know?
I tried following the syntax but it isn't working.

select languages from person where languages @> ARRAY['Java']::varchar[];
1
  • Getting this error - cannot resolve 'Java' given input columns:
    – Sandy
    Mar 22, 2019 at 6:40

3 Answers 3

11

You need to use a string constant on the left side, and the ANY operator on the array column:

select languages 
from person 
where 'Java' = any(languages);

This assumes languages is defined as text[] or varchar[] as your sample output indicates

2

try this

select languages from person where 'Java' = ANY (string_to_array(languages , ','))
1

You can search for more than one pattern replacing '=' operator by the regular expression match operator '~' preceding by a POSIX regular expression, such as:

select languages from person where '[Java,Php]' ~ ANY (string_to_array(languages , ','))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.