Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any MySQL functions to get all rows with with a start or end date that fall between a given start and end date?

Tags:

mysql

I have a table of events with a recorded start and end time as a MySQL DATETIME object (in the format YYYY-MM-DD HH:MM:SS. I want to find all events that occur in a specific date range. However, events can span multiple days (and go outside of my date range, but I want to return them if they even overlap by 1 second or more with my date range).

Suggestions?

like image 903
Thomas Owens Avatar asked Nov 10 '08 14:11

Thomas Owens


People also ask

What is the difference between now () and Current_date ()?

Current_date() will only give you the date. now() give you the datetime when the statement,procedure etc... started. sysdate() give you the current datetime.

Which function extracts date part of data MySQL?

The EXTRACT() function extracts a part from a given date.

How do I select a date range in MySQL?

How to Select rows from a range of dates with MySQL query command. If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';

How can I get records between two dates in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.


4 Answers

This will find every event that is completely contained inside the range:

SELECT * FROM table WHERE start_date BETWEEN start_of_range AND end_of_range 
                      AND stop_date  BETWEEN start_of_range AND end_of_range

This will find any events where any part of the event overlaps any part of the range:

SELECT * FROM table WHERE start_date <= end_of_range 
                      AND stop_date  >= start_of_range
like image 179
Robert Gamble Avatar answered Sep 19 '22 15:09

Robert Gamble


The answers by @Bill the Lizard and @Robert Gamble are correct for the question as asked, but I do wonder if you're asking what you think you are... If you're looking for overlapping events then you need to take into account events longer than your search range.

           Monday   Tuesday   Wednesday   Thursday

Search:                |-----------|

Shopping                       |-----|              Found OK
Eating              |--------|                      Found OK
Stack Overflow |---------------------------------|  Not found!

If you wanted to include SO, you'd do:

SELECT * FROM table WHERE (start_date < end_of_range AND end_date > start_of_range)

like image 30
Greg Avatar answered Sep 20 '22 15:09

Greg


SELECT *
FROM table
WHERE startdate >= 'starting date' AND startdate < 'ending date'
    OR enddate >= 'starting date' AND enddate < 'ending date'

should work for you.

Make sure you specify 'starting date' and 'ending date' with the time included.

'2008-01-01 00:00:00'' AND '2008-01-31 23:59:59'

This will help to avoid errors where dates are the same, but your time falls within the interval by a few hours, minutes, or seconds.

like image 38
Bill the Lizard Avatar answered Sep 17 '22 15:09

Bill the Lizard


Basically, you can use regular comparisons -- the ones above should work -- the trick is to check all the different cases that can occur.

A) events with an ending date within the range

B) events with a starting date within the range

C) events with both starting and ending dates within the range

D) events with both starting and ending dates outside the range, but overlapping it

Robert's answer is a good one, but it doesn't take into account case D, where the event starts before the range and ends after the range.

like image 20
Ilya Avatar answered Sep 18 '22 15:09

Ilya