Skip to main content
August 26, 2012
Question

SQL Date Range - Leap Year

  • August 26, 2012
  • 1 reply
  • 990 views

I have this query that runs a date range:

SELECT *

FROMPaymentMethods p

    LEFT JOIN showparsedemails o

        ON o.orderno = p.orderid

WHERE orderid IN (select orderno from pinpoint.dbo.ShowParsedEmails where orderdate >= '2/1/12' AND orderdate <= '2/29/12')

ORDER BY orderdate

This query will not return 2/29/12 dates unless I put the last date to 3/1/2012. Any other months, it will pick up any orders on the start or end date, but will not for leap year.

Any reason why it would do this? How can I fix it?

Thanks in advance.

This topic has been closed for replies.

1 reply

Inspiring
August 26, 2012

Is it just leap-year dates that are affected here, or any date?  You don't say, but are those columns plain DATEs, or are they DATETIMEs?  If they're DATETIMEs, then 2012-02-29 01:23:45 (for example) might not be considered <= 2012-02-29 (with an implicit 00:00:00 time component), depending on how you are querying, so will not match your filter.

BTW, make sure you're not hardcoding dynamic values in your SQL: parameterise them.  I suspect the code you've posted is just a simplified example though, so you might already be doing this.

--

Adam