Can Count(*) ever return null? [duplicate]
Asked Answered
G

5

15

Possible Duplicate:
Does COUNT(*) always return a result?

Is it possible in any scenario for the Count() function to return NULL?

Gules answered 11/2, 2011 at 14:46 Comment(2)
Discussed previously Does COUNT(*) always return a result?. never NULL: zero or no result at allGlyptograph
Here's an interesting related questionPammie
H
25

No, it will only return a zero (or non-zero) result. SqlServer will not return null.

Heaves answered 11/2, 2011 at 14:50 Comment(3)
Surely you mean "or non-zero and non-null"??Shirelyshirey
@cyberwiki - good point :-) Would be quite pointless if it always returned zero!Heaves
There are some cases where there is no result returned (if your select has a GROUP BY and there is no group). Depending on how you use your query, for instance if you use some ORM, this no result might be mapped to a null.Leix
P
7

According to the MSDN page, no.

That said:

For return values greater than 2^31-1, COUNT produces an error. Use COUNT_BIG instead.

Parlor answered 11/2, 2011 at 14:51 Comment(0)
R
4

No.

This will be 0

select count(*)
where 0=1

This will be 1

select count(*)
Rufusrug answered 11/2, 2011 at 14:52 Comment(0)
B
2

No it returns a zero if the table does not have any records in the table

Baum answered 11/2, 2011 at 14:54 Comment(0)
D
1

No. If the table or specific field being counted are empty it will return zero.

Dafodil answered 11/2, 2011 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.