I have a column which has the data like 2012-09-09 13:20:00, when I try to query the table, with the following query, it doesn’t include the data on the to date.
Procedure test (IN fromdate date, IN todate date)
Select * from table where date between fromdate and todate
Let’s say the user put in 2012-08-10 to 2012-09-09, then it doesn’t include the data on the 9th Sept. However, if I change the to date to 2012-09-10, then it will include the data. It seems like when there is no time included, mySQL defaults to midnight or 00:00:00
Is there a way to change it or a way around this? Such as, joining the time (23:59:59) to todate at some stage? i.e. using the following
set todate = todate.STR_TO_DATE("23:59:59");
You should use
>and<operators instead ofBETWEENas it is not inclusive with regards to dates (assuming your column isdatetime). If you only specify2011-01-31, it is the equivalent of2011-01-31 00:00:00.Example:
See it in action