SQL Server的时间跨度

时间:2011-09-21 08:27:25

标签: sql-server sql-server-2008

我有一个预订系统,在某个TimeSpan中我怎么能有很多RoomStatus?

离。

    ArrivalDate : 09-21-2011 4:00pm

    NumberOfHours : 3

    DepartureDate : 09-21-2011 7:00pm

    RoomNo : 10

    RoomStatus : Reserved

我希望RoomNo 10的状态仅在4:00到7:00之间保留?

你有想法吗?

我正在使用SQL Server 2008和VS 2010

修改

***RESERVATION TABLE***
PK-ReservationNo       bigint   
FK-ReservationTypeID   int  
PK-ClientID            bigint
FK-RoomNo              int
NoOfPersons            int  
NoOfHours              int  
ArrivalDate        smalldatetime
DepartureDate      smalldatetime

***ROOM TABLE***
PK-RoomNo           int
FK-RoomTypeNo       int
FK-RoomStatusId     int

2 个答案:

答案 0 :(得分:0)

尝试像这样转换您的查询;

if datediff(hour, ArrivalDate, DepartureDate) > NumberOfHours 
update('Empty') 
else
update ('Reserved')

答案 1 :(得分:0)

这可能是你想要的:

DECLARE @StartDate DateTime = '2011-01-01 12:00'
DECLARE @EndDate   DateTime = '2011-01-01 15:00'

--Free rooms between 12:00 and 15:00
SELECT *
FROM Room
WHERE NOT EXISTS(
    SELECT NULL
    FROM 
      Reservation 
    WHERE 
      ((ArrivalDate > @StartDate AND ArrivalDate < @EndDate) OR
      (DepartureDate > @StartDate AND DepartureDate < @EndDate))) AND
      Reservation.FK-RoomNo = Room.PK-RoomNo)