344

The following query:

SELECT * FROM `objects` 
WHERE (date_field BETWEEN '2010-09-29 10:15:55' AND '2010-01-30 14:15:55')

returns nothing.

I should have more than enough data to for the query to work though. What am I doing wrong?

2
  • 54
    Be cautious with BETWEEN, as both min and max values are considered to be in the range, to not process twice a date that is either the min and max value (edge case). For instance, the date 2010-09-29 00:00:00 will be between 2010-09-28 00:00:00 and 2010-09-29 00:00:00, AND ALSO between 2010-09-29 00:00:00 and 2010-09-30 00:00:00
    – minipif
    Mar 27, 2015 at 1:44
  • yea, what he said ^^ Nov 4, 2016 at 9:37

12 Answers 12

626

Your second date is before your first date (ie. you are querying between September 29 2010 and January 30 2010). Try reversing the order of the dates:

SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')

Official Docs: https://dev.mysql.com/doc/refman/8.0/en/datetime.html

5
  • I did know this but now has issue with UPDATE. I m trying to use BETWEEN for UPDATE should it work the same way?
    – Ingus
    Jun 2, 2017 at 9:55
  • 3
    @IngusGraholskis: a where clause should work the same on select or update statements.
    – T30
    Jun 30, 2017 at 12:37
  • 1
    Personally, I find the term 'from' and 'to' a sneaky way to remember the order in which the query should be written. Jan 21, 2019 at 22:04
  • How to return all data if both dates are null or empty? Jan 22, 2020 at 11:08
  • 1
    @Codingworld Just drop the WHERE Clause Jan 19, 2023 at 7:42
35

Your query should have date as

select * from table between `lowerdate` and `upperdate`

try

SELECT * FROM `objects` 
WHERE  (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
34

DATE() is a MySQL function that extracts only the date part of a date or date/time expression

SELECT * FROM table_name WHERE DATE(date_field) BETWEEN '2016-12-01' AND '2016-12-10';
1
  • you could add an explanation so people will understand what you are doing. Dec 15, 2016 at 6:39
27

Is date_field of type datetime? Also you need to put the eariler date first.

It should be:

SELECT * FROM `objects` 
WHERE  (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
22

As extension to the answer from @sabin and a hint if one wants to compare the date part only (without the time):

If the field to compare is from type datetime and only dates are specified for comparison, then these dates are internally converted to datetime values. This means that the following query

SELECT * FROM `objects` WHERE (date_time_field BETWEEN '2010-01-30' AND '2010-09-29')

will be converted to

SELECT * FROM `objects` WHERE (date_time_field BETWEEN '2010-01-30 00:00:00' AND '2010-09-29 00:00:00')

internally.

This in turn leads to a result that does not include the objects from 2010-09-29 with a time value greater than 00:00:00!

Thus, if all objects with date 2010-09-29 should be included too, the field to compare has to be converted to a date:

SELECT * FROM `objects` WHERE (DATE(date_time_field) BETWEEN '2010-01-30' AND '2010-09-29')
1
  • 1
    Thanks this was useful today. Sep 6, 2021 at 17:44
10

You can do it manually, by comparing with greater than or equal and less than or equal.

 select * from table_name where created_at_column  >=   lower_date  and  created_at_column <= upper_date;

In our example, we need to retrieve data from a particular day to day. We will compare from the beginning of the day to the latest second in another day.

  select * from table_name where created_at_column  >=   '2018-09-01 00:00:00'  and  created_at_column <= '2018-09-05 23:59:59';
2
  • 1
    This is the best option because A)Simplicity B)Readability
    – JuliSmz
    May 5, 2021 at 16:32
  • the comparison between the beginning and end of the day was important in my case
    – Larry
    May 14, 2021 at 15:21
4

Just Cast date_field as date

SELECT * FROM `objects` 
WHERE (cast(date_field as date) BETWEEN '2010-09-29' AND 
'2010-01-30' )
1
  • Awesome, spend almost 2 hours searching and trying and nothing worked out but your amazing solution, Thank you so much :)
    – Jodyshop
    Apr 13, 2021 at 0:13
4

I just tested on mariaDB with 2 cases as follows:

Case 1: SELECT * FROM table_name WHERE DATE(date_field) BETWEEN '2016-12-01' AND '2016-12-10' // include 2016-12-10
Case 2: SELECT * FROM table_name WHERE (date_field BETWEEN '2016-12-01' AND '2016-12-10') // not include 2016-12-10
3

When using Date and Time values, you must cast the fields as DateTime and not Date. Try :

SELECT * FROM `objects` 
WHERE (CAST(date_field AS DATETIME) 
BETWEEN CAST('2010-09-29 10:15:55' AS DATETIME) AND CAST('2010-01-30 14:15:55' AS DATETIME))
1

Might be a problem with date configuration on server side or on client side. I've found this to be a common problem on multiple databases when the host is configured in spanish, french or whatever... that could affect the format dd/mm/yyyy or mm/dd/yyyy.

1
  • 3
    The problem was the older date was listed before the newer date. Jun 24, 2015 at 20:09
-1

Try switching the dates around:

2010-09-29 > 2010-01-30?
-1

To display post(s) between 2 specific dates (for example):

an occasion starts on (04-12) and ends on (04-14) without selecting a year in query to make it recurrent every year on the specified dates, So my goal is to display that occasion on startdate and hide it automatically on enddate as follow:

$stmt = $db->query(
    "SELECT * FROM table

    WHERE (CAST(CURDATE() AS date)

    BETWEEN

    CAST(table.date_start AS date)
    AND
    CAST(table.date_end AS date))

    LIMIT 1"
);

Now, the occasion starts and disappear between these specified dates only, not after or before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.