Home » Script to count all NULLs in a table in SQL Server

Script to count all NULLs in a table in SQL Server

by Vlad Drumea
0 comments

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.

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.


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:

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

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.