deepak deepak - 2 months ago 8
SQL Question

how to get data between dates

I have following table with data:

id startdate finishdate
4 2015-09-17 2016-09-28
5 2016-08-17 2016-09-12


Now i need data for this date -
2016-02-02


For example if i enter this date
2016-02-02
then only 1st data will display.

Now if i enter date -
2016-08-10
then it will display both data.

i have tried with :

select * from customer where startdate >='2016-02-02' or finishdate<='2016-02-02'


but it is not working.

how can i achieve this ?

Answer

Your logic is simply backwards:

select c.*
from customer c
where startdate <= '2016-02-02' and finishdate >= '2016-02-02';

I advise you not to use between for dates. Here is a really good explanation by Aaron Bertrand on why that is a bad idea.