14

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
  • 29,632
  • 26
  • 117
  • 199
  • 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

23

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

Simon
  • 5,063
  • 1
  • 29
  • 43
  • 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
  • 1
    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
  • 344,413
  • 96
  • 627
  • 693
4

No.

This will be 0

select count(*)
where 0=1

This will be 1

select count(*)
Mikael Eriksson
  • 132,594
  • 21
  • 199
  • 273
2

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

Pinu
  • 6,940
  • 15
  • 51
  • 77
1

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

HLGEM
  • 91,883
  • 14
  • 110
  • 181