Reputation: 37
how to select data between 2 dates in between 2 dates? for example :
start_date - end_date -- title
2014-01-28 2014-02-03 test
2014-02-01 2014-02-15 ests
2014-02-28 2014-03-03 sets
2014-03-01 2014-03-10 sste
the problem is, i want to select data between 2014-02-02 and 2014-02-28 with first three of them selected because the first three data is included on 2nd month.
i tried this, but not works
SELECT title FROM my_tables WHERE start_date BETWEEN 2014-02-02 AND 2014-02-28
how it works?
Upvotes: 0
Views: 99
Reputation: 94914
A date range overlaps with another when either the start or the end is within that other range:
given range: |----| range 1 |----| overlaps with given range, because end_date is in that range range 2 |----| overlaps with given range, because start_date is in that range range 3 |----| does not overlap with given range, because both start and end date are not in that range
SELECT title FROM my_tables
WHERE (start_date BETWEEN '2014-02-02' AND '2014-02-28')
OR (end_date BETWEEN '2014-02-02' AND '2014-02-28');
Upvotes: 0
Reputation: 1269823
Two periods overlap when one begins before the other ends, and the other ends after the first begins. Here is the correct logic:
SELECT title
FROM my_tables
WHERE start_date <= '2014-02-28' and
end_date >= '2014-02-02';
Note that date constants need to be in single quotes.
Here is a SQL Fiddle showing it working.
Upvotes: 2
Reputation: 18600
Add single quote to date constant
SELECT title FROM my_tables WHERE start_date BETWEEN '2014-02-02' AND '2014-02-28'
Upvotes: 0