dmenesesg
dmenesesg

Reputation: 77

Verify if a value is in an array using postgresql

I have two columns, c_1 and c_2. The c_1, contains integers, and the other one, c_2, is the result of a listagg over numbers delimited by ,, (so the type is varchar)

I need to verify, row by row, if the value in c_1 is in c_2, How I can do this?

enter image description here

I already tried:

select user_id, c_1, c_2,
       case when c_1 = any(c_2) then 1 else 0 end as c_1_is_in_c_2
from table

However, I got the following error:

ERROR: op ANY/ALL (array) requires array on right side

Note: I am working with Amazon Redshift, which is based on PostgreSQL

Any help would be fully appreciated!

Upvotes: 0

Views: 150

Answers (1)

Ramkumar Pillai
Ramkumar Pillai

Reputation: 174

try something like this.

SELECT * FROM table WHERE array_field @> ARRAY[int_field];

Upvotes: 0

Related Questions