Используя BETWEEN
, вы можете получить ожидаемый результат:
SELECT *
FROM GuestInfo
WHERE (@StartPeriod BETWEEN ArrDate AND DepDate) AND
(@EndPeriod BETWEEN ArrDate AND DepDate)
или
SELECT *
FROM GuestInfo
WHERE ArrDate <= @StartPeriod AND DepDate >= @EndPeriod
Демо с данными образца:
DECLARE @GuestInfo TABLE (ID INT, Name VARCHAR (50), ArrDate DATE, DepDate DATE);
INSERT INTO @GuestInfo ( ID , Name, ArrDate, DepDate) VALUES
(100, 'P Smith' , '2013-02-16', '2013-02-20'),
(101, 'A Owen' , '2013-04-01', '2013-04-13'),
(108, 'M John' , '2013-04-05', '2013-04-08'),
(132, 'S Walter', '2014-06-09', '2014-06-11');
DECLARE @StartPeriod AS DATE = '2013-04-06';
DECLARE @EndPeriod AS DATE = '2013-04-08';
SELECT *
FROM @GuestInfo
WHERE (@StartPeriod BETWEEN ArrDate AND DepDate) AND
(@EndPeriod BETWEEN ArrDate AND DepDate)
Выход:
ID Name ArrDate DepDate
-----------------------------------
101 A Owen 2013-04-01 2013-04-13
108 M John 2013-04-05 2013-04-08