mickburkejnr
mickburkejnr

Reputation: 3690

Retrieve all records that occur within specific date range in MySQL

I have a table that contains several contracts, and each contract has a start date and an end date, like this:

|  ID   |   Contract Name   |  Start Date  |  End Date  |
|-------|-------------------|--------------|------------|
|   1   |  Joe Bloggs       |  2012-01-01  | 2012-02-05 |
|   2   |  John Smiths      |  2012-02-01  | 2012-02-20 |
|   3   |  Johnny Briggs    |  2012-03-01  | 2012-03-20 |

What I am trying to do is build a query that will retrieve contracts that were active between a specific time period. So if I had the start date of 2012-02-10 and an end date of 2012-03-21 I should have the following contracts displayed:

|  ID   |   Contract Name   |  Start Date  |  End Date  |
|-------|-------------------|--------------|------------|
|   2   |  John Smiths      |  2012-02-01  | 2012-02-20 |
|   3   |  Johnny Briggs    |  2012-03-01  | 2012-03-20 |

My problem though is that I don't know how to build the query to do this. This is what I've got so far:

SELECT *
FROM contracts c
WHERE c.startdate BETWEEN '2012-02-10'
    AND '2012-03-21'
    AND c.enddate BETWEEN '2012-02-10'
    AND '2012-03-21'

This doesn't work though, no records are retrieved. What am I doing wrong?

Upvotes: 2

Views: 2121

Answers (5)

Aris
Aris

Reputation: 5057

    SELECT * FROM contracts
    WHERE 
    (START_DATE between '2012-03-01' AND '2013-03-21')
    OR (END_DATE between '2012-03-01' AND '2013-03-21')
    OR (START_DATE<= '2012-03-01' AND END_DATE >='2013-03-21');

explanation:

(START_DATE between '2012-03-01' AND '2013-03-21')  

: intervals records that start between the input dates. First part or all of interval might be included.

(END_DATE between '2012-03-01' AND '2013-03-21') 

: intervals that end between the input dates. Last part or all of interval might be included.

(START_DATE<= '2012-03-01' AND END_DATE >='2013-03-21') 

: input dates are included within one interval only

Upvotes: 0

Vinayagam
Vinayagam

Reputation: 1002

SELECT * FROM contracts
                WHERE (START_DATE between '2012-03-01' AND '2013-03-21')
                OR (END_DATE between '2012-03-01' AND '2013-03-21')
                OR (START_DATE<= '2012-03-01' AND END_DATE >='2013-03-21');

Check the SQL fiddle

Upvotes: 4

Strawberry
Strawberry

Reputation: 33935

Er, time is linear right?

SELECT * 
FROM contracts 
WHERE end_date >= '2012-02-10' 
AND start_date <= '2012-03-21';

Let me illustrate...

    A-------------B
<------->
       <------>
           <----------->
<---------------------->

In all cases above, the start date is less than B. The end date is greater than A.

Upvotes: 3

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

It should have been like this

SELECT *
FROM contracts c
WHERE c.startdate >= '2012-02-10'
    AND c.enddate <= '2012-03-21'

Upvotes: 0

JoDev
JoDev

Reputation: 6873

For me, a good request will be

SELECT * FROM contracts c WHERE c.startdate >'2012-02-10' AND c.enddate < '2012-03-21'

Upvotes: 0

Related Questions