3

I'm running this simple check:

select * from mytable
where field_name = any(array['2']::_varchar);

field_name is _varcharso it's an array

but I'm getting this: ERROR: operator does not exist: character varying[] = character varying

What am I missing?

Thanks!

1
  • where '2' = any(field_name) Commented Aug 14, 2020 at 16:53

1 Answer 1

5

=ANY unwraps it RHS and compares them individually to the LHS, so it would be the same thing as field_name = '2'::varchar. You can't compare an array to a scalar like that. You want an operator that doesn't unwrapped the argument but compares arrays to each other:

field_name @> array['2']::_varchar

or

field_name && array['2']::_varchar

Or you want to leave the literal as a scalar, and then unwrap the other side which is already an array so it too becomes a scalar:

'2' =ANY (field_name)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.