Create a Query to check if any Column in a table is Null

那年仲夏 提交于 2020-12-27 06:23:04

问题


I have zero experience with SQL but am trying to learn how to validate tables. I am trying to see within a table if any of the columns are null.

Currently I have been going with a script that is just counting the number of nulls. I am doing this for each column. Is there a better script that I can use to check all the columns in a table?

select count(id) from schema.table where id is not null

If there are 100 records I would expect all columns to come back with 100 but if one column is null it will show a 0.


回答1:


You can count each column in a single query by using sum and case:

select
  sum(case when Column1 is null then 1 else 0 end) Column1NullCount
  , sum(case when Column2 is null then 1 else 0 end) Column2NullCount
  -- ...
  , sum(case when ColumnN is null then 1 else 0 end) ColumnNNullCount
from MyScheme.MyTable


来源:https://stackoverflow.com/questions/65239924/create-a-query-to-check-if-any-column-in-a-table-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!