1

array - user_zoned_ids

IF NULL = ANY(user_zones_ids) THEN        
       Do Something
END IF;

But IF condition is always giving false even if NULL is present in array

2
  • is null instead of = null ? Commented Aug 5, 2019 at 8:54
  • Not working - "ERROR: syntax error at or near "ANY" Commented Aug 5, 2019 at 9:02

1 Answer 1

2

You can't use the ANY operator to check for NULL values. You have to unnest the array and count the number of NULL elements:

if (select count(*) from unnest(user_zones_ids) as t(x) where x is null) > 0 then
   ... do something ...
end if;
Sign up to request clarification or add additional context in comments.

2 Comments

I wish Postgres introduces array.count(element) someday.
little bit faster expression select exists(select * from unnest(ARRAY[1,2,3, null]) g(v) where v is null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.