In this post I demo a script I put together to count NULLs in all columns of a given table in SQL Server and store the results in a table.
November 24th 2026 update
I’ve rewritten the script to do the count in a single table scan, improving performance for wide (20+ columns) tables.
And saving the results to a table is now optional and controlled by the @OutputToTable parameter.
Why?
I’ve seen variations of the question “how to count all NULLs in all columns of a table” pop up on reddit once every couple of months, and I figured I’d give it a shot and post here in case anyone else might need it in the future.
The script
Note: as with any script that reads an entire table multiple times, I recommend you avoid running it on busy production systems.
Just provide the table name in any of the following formats:
- TableName
- SchemaName.TableName
- [SchemaName].[TableName]
Every execution of the script has its own timestamp so you can run it multiple times for the same table if you want to track NULL counts over time.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | /* Get NULL counts for all columns in a table Author: Vlad Drumea More info: From https://github.com/VladDBA/SQL-Server-Scripts/ License https://github.com/VladDBA/SQL-Server-Scripts/blob/main/LICENSE.md */ /* Set the target table name Valid formats: TableName SchemaName.TableName [SchemaName].[TableName] */ DECLARE @TableName NVARCHAR(261) = 'Person.Person', @OutputToTable BIT = 0; /*Set to 1 to save results to table*/ /*Make sure the table name is valid*/ DECLARE @ObjectId INT = OBJECT_ID(@TableName,N'U'); IF @ObjectId IS NULL BEGIN RAISERROR('Please provide a valid table name',11,1) WITH NOWAIT; RETURN; END; /*Internal variables*/ DECLARE @FullQuotedTableName NVARCHAR(261) = QUOTENAME(OBJECT_SCHEMA_NAME(@ObjectId)) + N'.' + QUOTENAME(OBJECT_NAME(@ObjectId)), @FullTableName NVARCHAR(261) = OBJECT_SCHEMA_NAME(@ObjectId) + N'.' + OBJECT_NAME(@ObjectId), @AggList NVARCHAR(MAX), @ValuesList NVARCHAR(MAX), @LineFeed NVARCHAR(2) = CHAR(13) + CHAR(10), @SQL NVARCHAR(MAX); /*Create results table*/ IF OBJECT_ID(N'dbo.null_cols_table', N'U') IS NULL AND @OutputToTable = 1 BEGIN CREATE TABLE [dbo].[null_cols_table] ( [id] INT IDENTITY(1, 1) NOT NULL, [table_name] NVARCHAR(257), [column_name] NVARCHAR(128), [column_id] INT, [data_type] NVARCHAR(128), [nullable] BIT, [null_count] BIGINT, [record_count] BIGINT, [non_null_records] BIGINT, [time_of_check] DATETIME2(3) DEFAULT GETDATE() NOT NULL ); /*Create a clustered index on it*/ CREATE CLUSTERED INDEX [cix_null_cols_table] ON [dbo].[null_cols_table]([id]); END; WITH cols AS ( SELECT c.[column_id], c.[name], c.[is_nullable], [DataType] = CASE WHEN t.[name] IN (N'char', N'varchar', N'binary', N'varbinary') THEN t.[name] + N'(' + CASE WHEN c.[max_length] = -1 THEN N'max' ELSE CAST(c.max_length AS NVARCHAR(10)) END + N')' WHEN t.[name] IN (N'nchar', N'nvarchar') THEN t.[name] + N'(' + CASE WHEN c.[max_length] = -1 THEN N'max' ELSE CAST(c.max_length / 2 AS NVARCHAR(10)) END + N')' WHEN t.[name] IN (N'decimal', N'numeric') THEN t.[name] + N'(' + CAST(c.[precision] AS NVARCHAR(10)) + N',' + CAST(c.[scale] AS NVARCHAR(10)) + N')' WHEN t.[name] IN (N'datetime2', N'datetimeoffset', N'time') THEN t.[name] + N'(' + CAST(c.[scale] AS NVARCHAR(10)) + N')' ELSE t.[name] END FROM [sys].[columns] AS c JOIN [sys].[types] AS t ON t.[user_type_id] = c.[system_type_id] /*resolves alias types to their base system type*/ WHERE c.[object_id] = @ObjectId ) SELECT /*Only nullable columns need a COUNT_BIG expression*/ @AggList = STUFF(( SELECT N', COUNT_BIG(CASE WHEN ' + QUOTENAME([name]) + N' IS NULL THEN 1 END) AS n' + CAST([column_id] AS NVARCHAR(10)) FROM [cols] WHERE [is_nullable] = 1 ORDER BY [column_id] FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, N''), @ValuesList = STUFF(( SELECT N', (N''' + REPLACE([name], N'''', N'''''') + N''', ' + CAST([column_id] AS NVARCHAR(10)) + N', N''' + [DataType] + N''', CAST(' + CAST([is_nullable] AS NVARCHAR(1)) + N' AS bit), ' + CASE WHEN [is_nullable] = 1 THEN N'm.n' + CAST([column_id] AS NVARCHAR(10)) ELSE N'CAST(0 AS BIGINT)' END + N')' FROM [cols] ORDER BY [column_id] FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, N''); SET @AggList = ISNULL(@AggList + N', ', N'') + N'COUNT_BIG(*) AS rc'; SET @SQL = @LineFeed + N'SELECT @TableName AS [table_name],' + @LineFeed + N'v.[column_name],' + @LineFeed + N'v.[column_id],' + @LineFeed + N'v.[data_type],' + @LineFeed + N'v.[nullable],' + @LineFeed + N'v.[null_count],' + @LineFeed + N'm.[rc] AS [record_count],' + @LineFeed + N'm.[rc] - v.[null_count] AS [non_null_records]' + @LineFeed + N'FROM (SELECT ' + @AggList + N' FROM ' + @FullQuotedTableName + N') AS m' + @LineFeed + N'CROSS APPLY (VALUES ' + @ValuesList + N') AS v([column_name], [column_id], [data_type], [nullable], [null_count])' + @LineFeed + N'ORDER BY v.[column_id];'; IF @OutputToTable = 1 BEGIN SET @SQL = N'INSERT INTO [dbo].[null_cols_table] ([table_name], [column_name], [column_id], [data_type], [nullable], [null_count], [record_count], [non_null_records])' + @LineFeed + @SQL; END; /*SELECT @SQL AS [Generated SQL];*/ EXEC sys.sp_executesql @SQL, N'@TableName NVARCHAR(261)', @TableName = @FullTableName; IF @OutputToTable = 1 BEGIN SELECT * FROM [dbo].[null_cols_table] WHERE [table_name] = @FullTableName ORDER BY [nullable] DESC; END; |
You can also find this script in my SQL Server GitHub repo.
NULL count result set

The result set is pretty self explanatory.
null_count shows the number of records that are NULL for that specific column.
The result set returned is only for the current execution. Data from previous executions can be returned with:
| 1 2 | SELECT * FROM [null_cols_table]; |
Conclusion
This was more of query exercise for myself, but figured I’ll share for anyone else interested in having a count of the nulls in all columns of a table in SQL Server.
If you’re curios about other NULL-related stuff, fell free to check out this post about multiple NULLs in unique constraints