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

前端 未结 5 1183
耶瑟儿~
耶瑟儿~ 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条回答
  •  猫巷女王i
    2021-01-01 16:21

    One more construction, like @Clodoaldo Neto proposed. Just more compact expression:

    CREATE TEMPORARY TABLE null_arrays (
          id serial primary key
        , array_data int[]
    );
    
    INSERT INTO null_arrays (array_data)
    VALUES
          (ARRAY[1,2, NULL, 4, 5])
        , (ARRAY[1,2, 3, 4, 5])
        , (ARRAY[NULL,2, 3, NULL, 5])
    ;
    
    SELECT 
        *
    FROM 
        null_arrays
    WHERE
        TRUE = ANY (SELECT unnest(array_data) IS NULL)
    ;
    

提交回复
热议问题