2

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?

1
  • ID=2 the Start Date is before the start date you are searching for. Commented Mar 20, 2013 at 10:41

5 Answers 5

4
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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

For me, a good request will be

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

Comments

0

It should have been like this

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

Comments

0
    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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.