0

I am a table with three fields: id, startDate and endDate.

Now i need to get the records exactly in between the startDate and endDate.

For example let us assume i am having the table with the following records:

id startDate  endDate

1  2011-02-05 2011-02-10

2  2011-02-01 2011-02-06

3  2011-02-06 2011-02-08

For example startDate is 2011-02-05 and the end date is 2011-02-10 the output should be the following

id startDate  endDate

1  2011-02-05 2011-02-10

3  2011-02-06 2011-02-08

But according to my query second record is also appearing in the output. actually it should not...

My query is:

SELECT * FROM tblname WHERE startDate AND expiryDate BETWEEN '2011-02-05' AND '2011-02-10'

How to resolve this issue?

Thanks in advance..

1 Answer 1

3

You can't compare two columns to a range like that. The AND and OR operators connect boolean expressions. Your query is interpreted as "WHERE startDate is boolean true AND expiryDate is between these dates".

SELECT 
  * 
FROM 
  tblname 
WHERE 
  startDate BETWEEN '2011-02-05' AND '2011-02-10' 
AND 
  expiryDate BETWEEN '2011-02-05' AND '2011-02-10'
Sign up to request clarification or add additional context in comments.

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.