Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Counting values in table columns

Author  Topic 

dzabor
Posting Yak Master

138 Posts

Posted - 2013-02-01 : 16:21:01
I have a table with numerous columns, many of which are most likely not being used since moving from schema to schema and business practice to business practice over the years. I need to count the populated fields within the table to determine if we keep it or not moving forward.

ID, Demographic1,Demographic2,Demographic3,Demographic4,Demographic5

I need it to return the following:

ID, Demographic1,Demographic2,Demographic3,Demographic4,Demographic5
254 , 17 , 13, 0, 5, 200

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-01 : 17:23:17
If the non-populated values are NULL, you can do this:
SELECT
COUNT(ID),
COUNT(Demographic1),
COUNT(Demographic2),
COUNT(Demographic3),
COUNT(Demographic4),
COUNT(Demographic5)
FROM
TheTable
Go to Top of Page

dzabor
Posting Yak Master

138 Posts

Posted - 2013-02-01 : 20:25:03
Thanks!
Go to Top of Page
   

- Advertisement -