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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Count Columns That Contain Values

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-03-09 : 09:47:14
hi

How do I count numbers of columns that has value?

For example

ColA ColB ColC ColD ColE ColF
12 10.5 50 32 NULL NULL

In this example, I should have a count of 4

How should go about doing it?

many thanks.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-09 : 09:50:32
[code]CASE WHEN ColA IS NOT NULL THEN 1 ELSE 0 END
+ CASE WHEN ColB IS NOT NULL THEN 1 ELSE 0 END
+ CASE WHEN ColC IS NOT NULL THEN 1 ELSE 0 END
+ CASE WHEN ColD IS NOT NULL THEN 1 ELSE 0 END
+ . . .[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-03-09 : 10:05:56
hi

My apology, but how do I add up all the 1? Many thanks
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-03-09 : 10:06:49
sorry I didn't see the + sign. I thought it is a concat. Thanks

I got it now. Thanks
Go to Top of Page
   

- Advertisement -