Avatar of Mehram
Flag for Pakistan asked on

MS SQL Show All Empty Column from Particular Table

I am using MS SQL, at the time of development I have created numbers of column but on time to time tuning the some column have not data and all rows are empty, for example.

Table Name: Test

Name  Class   Section  Object
Xx         xx       Null      Null
Xxx      xxx      Null      Null
Xxx      xxx      Null      Null

I want to see trough sql statement after give particular table name, all those column name which are null, to decide either I need this column for future or delete for better performance of database

Please help


Avatar of undefined
Last Comment

8/22/2022 - Mon

Select count(*) from Test where Section is not null


Guy Hengel [angelIII / a3]

you have to check each individual column:

select count(*) from yourtable where section is not null
select count(*) from yourtable where object is not null
etc ...

for each query where the count(*) returns a value of 0, all rows have NULL value in that column.
Guy Hengel [angelIII / a3]


select count(*)
,sum(case when section is null then 0 else 1 end) as section_not_empty_count
,sum(case when object is null then 0 else 1 end) as object_not_empty_count
from yourtable
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Sir, actually I also do not know who many columns are created by me at the time of development, and the some columns are empty and no body have enter the data from the create date to till date. However now I am tuning my application and I want to check each table one by one:

So I can provide only table name and I want to see all those column which have no data in all rows.
Guy Hengel [angelIII / a3]

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

is there any link where i can learn about cursor solution?