Count the Null columns in a row in SQL

后端 未结 13 752
小蘑菇
小蘑菇 2020-11-30 04:36

I was wondering about the possibility to count the null columns of row in SQL, I have a table Customer that has nullable values, simply I want a query that return an int of

13条回答
  •  离开以前
    2020-11-30 05:35

    For ORACLE-DBMS only.

    You can use the NVL2 function:

    NVL2( string1, value_if_not_null, value_if_null )
    

    Here is a select with a similiar approach as Michael Berkowski suggested:

    SELECT (NVL2(col1, 0, 1) 
            + NVL2(col2, 0, 1)
            + NVL2(col3, 0, 1)
            ...
            ...
            + NVL2(col10, 0, 1)
            ) AS sum_of_nulls
    FROM table
    WHERE Customer=some_cust_id
    

    A more generic approach would be to write a PL/SQL-block and use dynamic SQL. You have to build a SELECT string with the NVL2 method from above for every column in the all_tab_columns of a specific table.

提交回复
热议问题