コメントで述べたように、次の 2 つのケースに関心があります。
ケース 1. 指定された車と日付について、車が利用可能かどうかを確認したい
DECLARE @car VARCHAR(20)
DECLARE @From DATETIME
DECLARE @To DATETIME
SET @car = 'your car'
SET @From = ..
SET @To = ..
SELECT CASE
WHEN (
SELECT COUNT(1) FROM booking
WHERE vehilce = @car
AND (@From >= vehilce.From AND @From <= vehilce.To)
OR (@T0 >= vehilce.From AND @T0 <= vehilce.To)
) >= 1 THEN "not available"
ELSE "available"
END
ケース 2. 特定の車について、車が予約されていない期間を取得したい
SELECT vehicle , previousTo ,nextFrom FROM
(
SELECT t1.vehicle AS vehicle, t1.From AS previosFrom, t1.To AS previousTo , t2.From AS nextFrom, t2.To AS nextTo
FROM booking t1
JOIN booking t2 ON t1.vehicle,t2.vehicle OR t1.To < t2.From
WHERE vehicle= @car
GROUP BY vehicle, previosFrom, previousTo
HAVING nextFrom = MIN (nextFrom)
) a