Querying Dates

  • Hello,

    When filtering for a date in a 'datetime' type field, I thought it was possible to just express the data portion of the data (as a string) in order to get the desired record(s). For example, if I am looking for a 'start_date' of 11/05/2006, and the record actually contains 2006-05-11 16:48:16.430, I thought I could just express the following in a WHERE clause;

    WHERE start_date = '11/05/2006'

    Will I need to CAST or use DATEPART, or is there some other way?

    Thank you for your help!

    CSDunn

  • Try the link below for the how and how to avoid the known issues.   Hope this helps.

    http://www.karaszi.com/SQLServer/info_datetime.asp

    Kind regards,

    Gift Peddie

    Kind regards,
    Gift Peddie

  • use this code at the query before you start working with the date parameter

    SET

    DATEFORMAT DMY --Day ,Month ,Year

  • Thank you for your assistance!

    CSDunn

  • If is really is a "start date", just use

    WHERE start_date >= '11/05/2006'

    If you just want the explicit date 11/05/2006, use

    WHERE CONVERT(VARCHAR(10), start_date, 101) = '11/05/2006'

     


    N 56°04'39.16"
    E 12°55'05.25"

  • This construct isn't so good, since placing a function on the datecolumn may prevent indexes to be used.

    Please see the link Gift Peddie posted on best practices on dates and dateformats in particular.

    '11/05/2006' is one of the ambigous formats that's better to not use in order to be as safe as possible.

    /Kenneth

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

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