Count the Null columns in a row in SQL

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

    The below script gives you the NULL value count within a row i.e. how many columns do not have values.

    {SELECT
        *,
        (SELECT COUNT(*)
        FROM (VALUES (Tab.Col1)
                    ,(Tab.Col2)
                    ,(Tab.Col3)
                    ,(Tab.Col4)) InnerTab(Col) 
            WHERE Col IS NULL) NullColumnCount
    FROM (VALUES(1,2,3,4)
                ,(NULL,2,NULL,4)
                ,(1,NULL,NULL,NULL)) Tab(Col1,Col2,Col3,Col4) } 
    

    Just to demonstrate I am using an inline table in my example.

    Try to cast or convert all column values to a common type it will help you to compare the column of different type.

提交回复
热议问题