Skip to content
Advertisement

SQL “between” not inclusive

I have a query like this:

But this gives no results even though there is data on the 1st.

created_at looks like 2013-05-01 22:25:19, I suspect it has to do with the time? How could this be resolved?

It works just fine if I do larger date ranges, but it should (inclusive) work with a single date too.

Advertisement

Answer

It is inclusive. You are comparing datetimes to dates. The second date is interpreted as midnight when the day starts.

One way to fix this is:

Another way to fix it is with explicit binary comparisons

Aaron Bertrand has a long blog entry on dates (here), where he discusses this and other date issues.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement