0

I have a query which checks how many records of a certain type are there using count().

select count(*) from abc where date="some value"

Here, is it possible that my query returns null or DBNull? Should I check for them?

karan k
  • 947
  • 2
  • 21
  • 45
  • 3
    A wise person once said, "an experiment is worth a thousand expert opinions." Try it out, try `where date="some value you know is not present"` – MushinNoShin Dec 03 '12 at 14:45

6 Answers6

5

I don't think so: it can return zero or greater, since you're counting.

NULL would be a wrong result, since no results is there're zero results.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
3

No, it will always return a number greater or equal to 0.

juergen d
  • 201,996
  • 37
  • 293
  • 362
1

Just try it:

SELECT COUNT(*) WHERE 1=2
--Returns 0

Per MSDN:

COUNT always returns an int data type value.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
0

If you are using ExecuteScalar, I think the call will retrieve null instead of a DbNull object.

What I use to do in order to always receive a value is encapsulating my request: SELECT ISNULL((SELECT COUNT(date) FROM abc WHERE date = "some value"), 0)

Serge

Serge Bollaerts
  • 324
  • 2
  • 6
0

You should check DbNull as it is database oriented.

See this answer

Community
  • 1
  • 1
charles
  • 55
  • 8
0

This is what i do:

if (rdr.HasRows)
        {
          rdr.Read();
          if (rdr["MyField"] != DBNull.Value)
                {
                   bla bla....
                }

So yes check for DBNull.

e4rthdog
  • 5,103
  • 4
  • 40
  • 89