9


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: Commented Mar 22, 2019 at 6:40

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

Comments

3

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 , ','))

Comments

2

try this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.