Count the Null columns in a row in SQL

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

    You don't state RDBMS. For SQL Server 2008...

    SELECT CustomerId,
           (SELECT COUNT(*) - COUNT(C)
            FROM   (VALUES(CAST(Col1 AS SQL_VARIANT)),
                          (Col2),
                          /*....*/
                          (Col9),
                          (Col10)) T(C)) AS NumberOfNulls
    FROM   Customer  
    

提交回复
热议问题