Count the Null columns in a row in SQL

后端 未结 13 761
小蘑菇
小蘑菇 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:22

    There isn't a straightforward way of doing so like there would be with counting rows. Basically, you have to enumerate all the columns that might be null in one expression.

    So for a table with possibly null columns a, b, c, you could do this:

    SELECT key_column, COALESCE(a,0) + COALESCE(b,0) + COALESCE(c,0) null_col_count
      FROM my_table
    

提交回复
热议问题