Skip to content
Advertisement

How to find which columns don’t have any data (all values are NULL)?

I have several tables in a database. I would like to find which columns (in which tables) don’t have any values (all NULL in a column). I the example below, the result should be

I don’t have any idea how to create this kind of query. Your help is most appreciated!

Advertisement

Answer

For a single column, count(ColumnName) returns the number of rows where ColumName is not null:

You can generate a query for all columns. Per Martin’s suggestion, you can exclude columns that cannot be null with is_nullable = 1. For example:

If the number of tables is large, you can generate a query for all tables in a similiar way. The list of all tables is in sys.tables.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement