113

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,611
  • 17
  • 32
  • 36
Oto Shavadze
  • 37,634
  • 51
  • 140
  • 215

3 Answers3

151

Try

SELECT * FROM table WHERE arr @> ARRAY['s']::varchar[]
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Wojtas
  • 2,134
  • 1
  • 16
  • 9
82

Note that this may also work:

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

Compare two arrays for containment.

vol7ron
  • 38,313
  • 20
  • 110
  • 168