• anthony.green (1/17/2013)


    Ed Wagner (1/17/2013)


    If your field is a datetime data type, then you're comparing the datetime result of GetDate() against the datetime value of the field. So, nothing's going to match. I like the BETWEEN or <= and >= approach as well. That was you can avoid putting date parsing functions on the field (i.e.: to the left of the = sign) which would be a disaster for performance.

    Effectively, you want use date functions that evaluate to something like this:

    WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'

    AND '01/01/2013 23:59:59.999'

    That will equate to BETWEEN 01/01/2013 and 02/01/2013 due to the rounding of datetime.

    You would want 01/01/2013 00:00:00.000 AND 01/01/2013 23:59:59.997 instead, unless your using datetime2

    But always try to remember to qualify your dates as ISO formats so that SQL doesnt get confused or set the dateformat to the correct format

    YYYY-MM-DDTHH:MM:SS.ms

    Take 02/01/2013 is it 2nd Jan 2013 or 1st Feb 2013, depending who you ask you will get a different answer, where as if it follows an ISO standard there cannot be any confusion

    http://en.wikipedia.org/wiki/ISO_8601

    It would be even better to specify start and end dates using the "half-open interval" model, like this:

    WHERE DatetimeColumn >= '2013-01-01 00:00:00.000' AND DatetimeColumn < '2013-01-02 00:00:00.000'

    This will return all values with a date component of 2013-01-01 with none of the ambiguity that can result from rounding the time component when evaluating the BETWEEN condition.

    It's called a "half-open interval" because the "start" datetime is included in the range but the "end" datetime is not.

    Jason Wolfkill