How to make a select with array contains value clause in psql

PostgresqlPostgresql 9.2

Postgresql 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOto ShavadzeView Question on Stackoverflow
Solution 1 - PostgresqlWojtasView Answer on Stackoverflow
Solution 2 - PostgresqlAetherUnboundView Answer on Stackoverflow
Solution 3 - Postgresqlvol7ronView Answer on Stackoverflow