Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

null query Expand / Collapse
Author
Message
Posted Wednesday, March 5, 2014 6:07 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 3:08 AM
Points: 68, Visits: 409
why the below query showing zero even if there
are 2 records in age column with null value

select count(age) from dbo.prim
where age is null
while the same table queried with
the below query works
giving 2 records

select count(name) from dbo.prim
where age is null
Post #1547746
Posted Wednesday, March 5, 2014 6:17 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 13,724, Visits: 10,680
According to BOL (emphasis is mine):

COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.


ALL is the default.

COUNT (Transact-SQL)




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1547752
Posted Wednesday, March 5, 2014 6:19 AM This worked for the OP Answer marked as solution


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:58 PM
Points: 2,290, Visits: 2,752
When you look at your messages window you'll notice the message "Warning: Null value is eliminated by an aggregate or other SET operation." which will explain your situation.

It is because you are counting the item in the column containing the NULL values. These NULL values will (default) be eliminated in the count. To get the desired results you can count on another column or all columns. See the example below:

create table #test (id int, value  int)
insert into #test
select 1,1
union all select 2,null
union all select 3,null
union all select 4,2

select
count(value) as total_NULL_column
, count(ID) as total_ID_column
, count(*) as total_all_columns
from #test
where value is null

drop table #test



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1547753
Posted Wednesday, March 5, 2014 6:27 AM This worked for the OP Answer marked as solution


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:25 PM
Points: 43,008, Visits: 36,164
If you want to count the number of rows where the age is null, you probably want this:

select count(*) from dbo.prim
where age is null

Count(*) means count the number of rows. Count(<column name>) means count the number of rows where that column has a non-null value.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1547757
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse