dbTalk Databases Forums  

SQL help again

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL help again in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Phil Hunt
 
Posts: n/a

Default SQL help again - 10-02-2011 , 11:14 AM






How do I count a column that is NULL without CASE. I was looking at NULLIF
and ISNULL, they can't seem to do the job.

Thanks

Sorry about double posting, I replied to my question by mistake.

Reply With Quote
  #2  
Old   
Phil Hunt
 
Posts: n/a

Default Re: SQL help again - 10-02-2011 , 11:48 AM






I tried
CASE EventID when NULL then 1 else 0 end

It always return 0, why ?


"Phil Hunt" <aaa (AT) aaa (DOT) com> wrote

Quote:
How do I count a column that is NULL without CASE. I was looking at NULLIF
and ISNULL, they can't seem to do the job.

Thanks

Sorry about double posting, I replied to my question by mistake.

Reply With Quote
  #3  
Old   
Bob Barrows
 
Posts: n/a

Default Re: SQL help again - 10-02-2011 , 12:06 PM



Phil Hunt wrote:
Quote:
I tried
CASE EventID when NULL then 1 else 0 end

It always return 0, why ?


Because the way you have the statement structured is causing an equality
comparison to be performed, the result of which will always be false. You
have to structure it like:

CASE when EventID IS NULL then 1 else 0 end

To answer your original question, you can subtract the result of
count(distinct column_name) from count(*):

select count(*) - count(distinct eventid) as null_eventids

Reply With Quote
  #4  
Old   
phil hunt
 
Posts: n/a

Default Re: SQL help again - 10-02-2011 , 05:44 PM



That's it.Thanks.


"Bob Barrows" <reb01501 (AT) NOSPAMyahoo (DOT) com> wrote

Quote:
Phil Hunt wrote:
I tried
CASE EventID when NULL then 1 else 0 end

It always return 0, why ?


Because the way you have the statement structured is causing an equality
comparison to be performed, the result of which will always be false. You
have to structure it like:

CASE when EventID IS NULL then 1 else 0 end

To answer your original question, you can subtract the result of
count(distinct column_name) from count(*):

select count(*) - count(distinct eventid) as null_eventids




Reply With Quote
  #5  
Old   
--CELKO--
 
Posts: n/a

Default Re: SQL help again - 10-03-2011 , 10:55 AM



SELECT COUNT(*)- COUNT(event_id) AS null_even_cnt
FROM Events;

Reply With Quote
  #6  
Old   
Bob Barrows
 
Posts: n/a

Default Re: SQL help again - 10-03-2011 , 11:09 AM



Bob Barrows wrote:
Quote:
To answer your original question, you can subtract the result of
count(distinct column_name) from count(*):

select count(*) - count(distinct eventid) as null_eventids
Oh dear. Why did I put that "distinct" in there? I have no idea what I was
thinking when I typed that. The correct statement is as CELKO wrote:
select count(*) - count(eventid) as null_eventids

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.