Skip to content
Advertisement

Number of Null and non-null values for each column in SQL for a given table

I am trying to find the number of null values, non-null values, total rows for each column in a table named StudentScore.

Code to create table:

CREATE TABLE StudentScore
    ( Student_ID INT PRIMARY KEY,
      Student_Name NVARCHAR (50),
      Student_Score INT  ) 
    GO  INSERT INTO StudentScore VALUES (1,'Ali', NULL)
    INSERT INTO StudentScore VALUES (2,'Zaid', 770)
    INSERT INTO StudentScore VALUES (3,'Mohd', 1140)
    INSERT INTO StudentScore VALUES (4,NULL, 770)
    INSERT INTO StudentScore VALUES (5,'John', 1240)
    INSERT INTO StudentScore VALUES (6,'Mike', 1140)
    INSERT INTO StudentScore VALUES (7,'Goerge', NULL)

Code to find number of null, non null, total rows for each column in the table:

DECLARE @sql NVARCHAR(MAX);

SELECT @sql = STRING_AGG(
  FORMATMESSAGE('SELECT table_schema = ''%s''
                        ,table_name = ''%s''
                        ,table_col_name = ''%s'' 
                        ,row_num = COUNT(*)
                        ,row_num_non_nulls = COUNT(%s)
                        ,row_num_nulls = COUNT(*) - COUNT(%s)
                 FROM %s.%s', 
     QUOTENAME(dbo),
     QUOTENAME(StudentScore),
     QUOTENAME(COLUMN_NAME),
     QUOTENAME(COLUMN_NAME),
     QUOTENAME(COLUMN_NAME),
     QUOTENAME(dbo),
     QUOTENAME(StudentScore),
     QUOTENAME(COLUMN_NAME)), ' UNION ALL' + CHAR(13)
               ) WITHIN GROUP(ORDER BY dbo, StudentScore)

FROM INFORMATION_SCHEMA.COLUMNS
WHERE IS_NULLABLE = 'YES';

SELECT @sql;
EXEC(@sql);

I am getting below errors:

Msg 207, Level 16, State 1, Line 11
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'StudentScore'.
Msg 207, Level 16, State 1, Line 16
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'StudentScore'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'StudentScore'.

Result I need:

| table_schema  |   table_name    | table_col_name   | row_num  | row_num_non_nulls  | row_num_nulls |
+---------------+-----------------+------------------+----------+--------------------+---------------+
| [dbo]         | [StudentScore]  | [Student_Name]   |       7  |                 6  |             1 |
| [dbo]         | [StudentScore]  | [Student_Score]  |       7  |                 5  |             2 |

Advertisement

Answer

The reason for the error is that INFORMATION_SCHEMA.COLUMNS does not contain columns dbo and StudentTable, so using QUOTENAME(dbo), QUOTENAME(StudentScore) and WITHIN GROUP(ORDER BY dbo, StudentScore) is an eror.

If you want to count the NULL and NOT NULL values for NULLABLE columns for one specific table, the statement should be:

SELECT @sql = STRING_AGG(
    FORMATMESSAGE('
       SELECT table_schema = ''%s''
       ,table_name = ''%s''
       ,table_col_name = ''%s'' 
       ,row_num = COUNT(*)
       ,row_num_non_nulls = COUNT(%s)
       ,row_num_nulls = COUNT(*) - COUNT(%s)
       FROM %s.%s
       ', 
       QUOTENAME(c.TABLE_SCHEMA),
       QUOTENAME(c.TABLE_NAME),
       QUOTENAME(c.COLUMN_NAME),
       QUOTENAME(c.COLUMN_NAME),
       QUOTENAME(c.COLUMN_NAME),
       QUOTENAME(c.TABLE_SCHEMA),
       QUOTENAME(c.TABLE_NAME)
     ), 
     ' UNION ALL '
) WITHIN GROUP(ORDER BY c.TABLE_SCHEMA, c.TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE 
   (c.TABLE_SCHEMA = 'dbo') AND 
   (c.TABLE_NAME = 'StudentScore') AND
   (c.IS_NULLABLE = 'YES');

SELECT @sql;
EXEC (@sql);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement