I am getting confusing in writing the best stored procedure for my requirement.
I am filtering records based on StartDate
and EndDate
.
So I want to get records whose StartDate
and EndDates
are between StartDate
& EndDate
range.
Previously I had asked this question Here in SE, and got some answers. But later I found that the query is not giving me correct results.
So the answer I found there is:
select *
from tbl
Where (Event_Startdate between @StartDate and @EndDate)
and (Event_Enddate between @StartDate and @EndDate)
Lets say I have records :
If I execute the above query with inputs like:
@StartDate = '1/1/2000',
@EndDate = '5/16/2014',
The query above gives no records at all right.. because here Event_StartDate
is between the StartDate
and EndDate
, where as End Date is not between the range.
But what I want is if the StartDate
or EndDate
is between range, then it should be selected.
Then immediately OR operator came into my mind, I tried by changing And to OR in my query
select *
from tbl
Where (Event_Startdate between @StartDate and @EndDate)
OR (Event_Enddate between @StartDate and @EndDate)
It gave the correct answer in this situation.
But the problem with this query is it doesn't check the second condition, which is after 'OR' If the first condition is true. Suppose if i gave input as:
@StartDate = '1/1/2000',
@EndDate = '7/25/2014'
It will select all records in the table because StartDate is in between 1/1/2000 to 7/25/2014 which is true, so it does nt check whether EndDate is in between the range or not, it does'nt care about EndDate.
I hope my problem is clear to u..Please somebody help with this...