MSSQL 判断一个时间段是否在另一个时间段内!
1 CREATE TABLE #B
(
MeetingRoom int,
BeginTime datetime,
EndTime datetime
)
insert into #B
select 1,'2012-05-24 10:00:00','2012-05-24 16:00:00'
DECLARE @BeignTime datetime --查询开始时间
DECLARE @EndTime datetime --查询结束时间
DECLARE @MeetingRoom INT --查询的会议室
SET @BeignTime='2012-05-24 00:00:00'
SET @EndTime='2012-05-24 17:00:00'
SET @MeetingRoom=1
SELECT @BeignTime=DATEADD(SECOND,1,@BeignTime) --解决结束时间和开始时间相同的冲突
SELECT CASE WHEN COUNT(1)>0 THEN '已占用' ELSE '未占用' END FROM #B WHERE
(@BeignTime BETWEEN BeginTime AND EndTime OR
@EndTime BETWEEN BeginTime AND EndTime OR
BeginTime BETWEEN @BeignTime AND @EndTime OR
EndTime BETWEEN @BeignTime AND @EndTime) AND MeetingRoom=@MeetingRoom
DROP TABLE #B