15

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

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

Community
  • 1
  • 1
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • 3
    Discussed previously [Does COUNT(*) always return a result?](http://stackoverflow.com/questions/2552086/does-count-always-return-a-result). never NULL: zero or no result at all – gbn Feb 11 '11 at 15:23
  • Here's an interesting related [question](http://stackoverflow.com/questions/4115710/count-returns-null) – rosscj2533 Feb 11 '11 at 15:35

5 Answers5

25

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

Simon
  • 5,373
  • 1
  • 34
  • 46
  • Surely you mean "or non-zero and non-null"?? – RichardTheKiwi Feb 12 '11 at 22:04
  • @cyberwiki - good point :-) Would be quite pointless if it always returned zero! – Simon Feb 14 '11 at 11:01
  • 2
    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`. – kord Jun 03 '20 at 21:16
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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
4

No.

This will be 0

select count(*)
where 0=1

This will be 1

select count(*)
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
2

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

Pinu
  • 7,310
  • 16
  • 53
  • 77
1

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

HLGEM
  • 94,695
  • 15
  • 113
  • 186