How to make a select with array contains value clause in psql
PostgresqlPostgresql 9.2Postgresql Problem Overview
I have column arr
which is of type array
.
I need to get rows, where arr
column contains value s
This query:
SELECT * FROM table WHERE arr @> ARRAY['s']
gives the error:
> ERROR: operator does not exist: character varying[] @> text[]
Why does it not work?
p.s. I know about any()
operator, but why doesn't @>
work?
Postgresql Solutions
Solution 1 - Postgresql
Try
SELECT * FROM table WHERE arr @> ARRAY['s']::varchar[]
Solution 2 - Postgresql
Note that this may also work:
SELECT * FROM table WHERE s=ANY(array)
Solution 3 - Postgresql
SELECT * FROM table WHERE arr && '{s}'::text[];
Compare two arrays for containment.