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 2005 Forums
 Transact-SQL (2005)
 Counting NULL

Author  Topic 

harlingtonthewizard
Constraint Violating Yak Guru

352 Posts

Posted - 2008-10-08 : 01:52:31
If I have a result as below how do I count the number of EventCommentID where IS NULL? So result should be 8.

EventCommentID
1
2
4
3
NULL
5
6
NULL
NULL
NULL
NULL
NULL
NULL
NULL

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-08 : 01:55:20
[code]SELECT SUM(CASE WHEN EventCommentID IS NULL THEN 1 ELSE 0 END) AS NullCount
FROM Table[/code]
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-10-08 : 06:14:24
Hi Visakh, why not

SELECT COUNT(*)
FROM Table
WHERE EventID IS NULL

works for me...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-08 : 06:20:53
even this will do

SELECT COUNT(*)-COUNT(EventID)
FROM Table
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-10-08 : 06:39:49
Evil one. But my execution plan is shorter.
Go to Top of Page
   

- Advertisement -