Date comparision

  • Hi,

    Could some one please explain the differences (if any) between these two queries

    Query 1:

    SELECT * FROM my_table

    WHERE created_dt >= '12/15/2006'

    vs

    Query 2:

    SELECT * FROM my_table

    WHERE created_dt >= DATEADD(day, DATEDIFF(day, 0, '12/15/2006'), 0)

    NOTE: created_dt is a DATETIME data type.

    Using any of the above queries I got the same results.

    Thanks,

    Tuan

  • They should be identical.  The datadd... part is used to strip the part time of the date.  However in this case it seems to be useless.  But I also assume that the running code is different than this one so don't take my word for it and test to be certain.

     

    Sample or the strip time :

    Select DATEADD(day, DATEDIFF(day, 0, GetDate()), 0)

  • Ninja has covered the question, I have just one more thing to add : it is recommended to use YYYYMMDD format when entering dates (without any delimiters). This is the only way when there can be no confusion about the date, as the interpretation does not depend on regional (language) settings.

Viewing 3 posts - 1 through 2 (of 2 total)

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