Querying NULL dates

  • Is it redundant to write something like this in the where clause............?

    where AdmitDateTime is not null
    and AdmitDateTime>='2016-04-01'

  • Yes.  

    Try it yourself.  

    Leave the WHERE date IS NOT NULL off, and see if you get the same number of rows.

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • Thanx.

  • NineIron - Wednesday, April 26, 2017 12:15 PM

    Is it redundant to write something like this in the where clause............?

    where AdmitDateTime is not null
    and AdmitDateTime>='2016-04-01'

    Yes, it is redundant.  Any comparison to NULL will always return NULL.
    To compare NULL, you need to use 

    WHERE AdmitDateTime IS [NOT] NULL

    The following queries return exactly the same results.
    Even the query plans are the same.

    CREATE TABLE #Test (
      ID INT IDENTITY(1,1)
    , AdmitDateTime DATETIME
    );

    INSERT INTO #Test (AdmitDateTime)
    VALUES ('2016-03-01'), ('2016-04-01'), ('2016-05-01'), (NULL);

    SELECT *
    FROM #Test
    WHERE AdmitDateTime IS NOT NULL
    AND AdmitDateTime >= '2016-04-01';

    SELECT *
    FROM #Test
    WHERE AdmitDateTime >= '2016-04-01';

    DROP TABLE #Test;

  • Thanx.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply