I have an ID field two date fields in table named as From_Date and To_Date. I want to select this dates with date criteria.
ID From_Date To_Date
1 2011-05-03 2011-05-28
2 2011-04-29 2011-05-10
3 2011-05-09 2011-05-20
4 2011-04-28 2011-05-09
5 2011-04-29 2011-05-19
6 2011-05-09 2011-05-09
7 2011-05-09 2011-05-09
8 2011-05-09 2011-05-09
9 2011-05-21 2011-05-21
10 2011-06-06 2011-06-06
11 2011-05-21 2011-05-28
12 2011-05-14 2011-05-28
13 2011-05-23 2011-05-24
14 2011-05-27 2011-05-27
15 2011-05-2开发者_如何学运维3 2011-05-24
Now I want two parameter for condition of start date and end date like
@StartDate = '05/01/2011'
@EndDate = '05/10/2011'
And I want Output from these parameters when ever array of dates between startdate and enddate parameter should be compare to array of dates between From_Date and To_Date field
Output:
ID From_Date To_Date
1 2011-05-03 2011-05-18
2 2011-04-29 2011-05-10
3 2011-05-09 2011-05-20
4 2011-04-28 2011-05-09
5 2011-04-29 2011-05-19
6 2011-05-09 2011-05-09
7 2011-05-09 2011-05-09
8 2011-05-09 2011-05-09
is this possible in single query instead use of trigger or function?
If you want to get all records which have From_Date
or To_Date
are in between your date range (@StartDate and @EndDate)
then it's simple query like :
SELECT * FROM TableName
WHERE (From_Date BETWEEN @StartDate AND @EndDate) OR (To_Date BETWEEN @StartDate AND @EndDate);
select *
from YourTable
where From_Date <= @EndDate and
To_Date >= @StartDate
https://data.stackexchange.com/stackoverflow/q/113761/
精彩评论