How to determine if NULL is contained in an array in Postgres?

前端 未结 5 1169
耶瑟儿~
耶瑟儿~ 2021-01-01 15:41

How do I determine if NULL is contained in an array in Postgres? Currently using Postgres 9.3.3.

If I test with the following select it returns contains_null =

5条回答
  •  时光取名叫无心
    2021-01-01 16:22

    i didn't want to use unnest either, so i used a comparison of array_length using array_remove to solve a similar problem. Tested on 9.4.1, but should work in 9.3.3.

    SELECT
    ARRAY_LENGTH(ARRAY[1,null], 1) > ARRAY_LENGTH(ARRAY_REMOVE(ARRAY[1,null], NULL), 1) 
    OR ARRAY_LENGTH(ARRAY_REMOVE(ARRAY[1,null], NULL), 1) IS NULL
    ---------
    t
    

提交回复
热议问题