120

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?

CDspace
  • 2,639
  • 18
  • 30
  • 36
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

3 Answers3

157

Try

SELECT * FROM table WHERE arr @> ARRAY['s']::varchar[]
Wojtas
  • 2,254
  • 1
  • 17
  • 9
92

Note that this may also work:

SELECT * FROM table WHERE s=ANY(array)
AetherUnbound
  • 1,714
  • 11
  • 10
20
SELECT * FROM table WHERE arr && '{s}'::text[];

Compare two arrays for containment.

vol7ron
  • 40,809
  • 21
  • 119
  • 172