попробуйте это:
--needed for each query run
DECLARE @YourTable table (RowID int, FromDate datetime, ToDate datetime)
DECLARE @datefrom datetime
,@dateto datetime
INSERT INTO @YourTable VALUES (1,'2010-01-10', '2010-01-13')
INSERT INTO @YourTable VALUES (2,'2009-03-20', '2010-01-09')
первый пример
--be sure to use the DECLAREs and INSERTs from above with this
SELECT @datefrom='2000-01-01'
,@dateto='2009-03-21'
--query to that only checks if the FromDate is between the given @datefrom and @dateto, which is necessary for the OPs first example
SELECT
*
FROM @YourTable
WHERE FromDate>=@datefrom AND FromDate<@dateto+1
ВЫХОД:
RowID FromDate ToDate
----------- ----------------------- -----------------------
2 2009-03-20 00:00:00.000 2010-01-09 00:00:00.000
(1 row(s) affected)
второй пример
--same query as above, but run with example two dates
--be sure to use the DECLAREs and INSERTs from above with this
SELECT @datefrom='2009-01-01'
,@dateto='2011-01-01'
SELECT
*
FROM @YourTable
WHERE FromDate>=@datefrom AND FromDate<@dateto+1
ВЫХОД:
RowID FromDate ToDate
----------- ----------------------- -----------------------
1 2010-01-10 00:00:00.000 2010-01-13 00:00:00.000
2 2009-03-20 00:00:00.000 2010-01-09 00:00:00.000
(2 row(s) affected)
Альтернативные запросы, чтобы убедиться, что весь диапазон FromDate + ToDate находится в пределах указанных значений @datefrom и @dateto, используют этот запрос:
--be sure to use the DECLAREs and INSERTs from above with this SELECT @datefrom='2009-03-22'
,@dateto='2011-01-01'
SELECT
*
FROM @YourTable
WHERE FromDate>=@datefrom AND ToDate<@dateto+1
ВЫХОД:
RowID FromDate ToDate
----------- ----------------------- -----------------------
1 2010-01-10 00:00:00.000 2010-01-13 00:00:00.000
(1 row(s) affected)