Search code examples
sqldatecomparator

SQL Server less than or equal to (<=) date not including last date


I have a what seems to be a simple but odd finding. When I use:

WHERE DateTime >= '2024-04-01' AND DateTime <= '2024-04-30'

This above only gives me up to 04/29, but not 04/30

WHERE DateTime >= '2024-04-01' AND DateTime < '2024-04-30'

This also only gives me up to 04/29, but not 04/30\

*And it is definitely not because 04/30 does not have any data, otherwise not need for bothering to post. 04/30 appears when I edit the WHERE to

AND DateTime < '2024-05-01'

And similarly if I use <= '2024-05-01', it also gives me up to 04/30 but not 05/01

Which is weird, why is <= treated the same way as <?

Thank you!


Solution

  • They are not the same. When using <= the value 2024-04-30 is only included if you have a DateTime value of '2024-04-30 00:00:00' exactly. Using < will exclude 2024-04-30 completely. See also How to compare datetime with only date in SQL Server.