dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Compare Dates in SQL, one date is NULL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    [RESOLVED] Compare Dates in SQL, one date is NULL

    hello, I need to return results depending on a condition which a date field is greater (newer) than other date field. using this for example:

    select * from mytable where mydate1>olddate2;

    the problem is that all results are returned because olddate2 rows are all NULL because its constructed to accept null, and no date entered yet in that field. what is the solution?

    Thank's

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: Compare Dates in SQL, one date is NULL

    I want to count mydate1 as greater (newer) when other field mydate2 either has NULL value or a date that actually older than date stored in mydate1

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,866

    Re: Compare Dates in SQL, one date is NULL

    First I am assuming that this is MS SQL - you didn't tell us that fact and it's kind of important to the syntax we suggest.

    Why wouldn't this work?

    Code:
    select * from mytable
        where mydate1>olddate2
                 or olddate2 is null;
    Although I must say your description of the problem is a little confusing...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: Compare Dates in SQL, one date is NULL

    OMG! I forget that simple solution! Thank you very much for your help. SOLVED!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width