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?
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?
 
    
    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. 
 
    
    Just try it:
SELECT COUNT(*) WHERE 1=2
--Returns 0
Per MSDN:
COUNT always returns an int data type value.
 
    
    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
 
    
    This is what i do:
if (rdr.HasRows)
        {
          rdr.Read();
          if (rdr["MyField"] != DBNull.Value)
                {
                   bla bla....
                }
So yes check for DBNull.
