22,294
社区成员
发帖
与我相关
我的任务
分享
declare @t table(sid int, etime smalldatetime , stime smalldatetime)
insert into @t
select '111', '2013-3-7 7:12:00', '2013-3-7 8:13:00' union all
select '222', '2013-3-7 9:00:00','2013-3-7 10:10:00'union all
select '333', '2013-3-7 11:14:00 ','2013-3-7 12:11:00'
select *,convert(char(5),etime,108) as 起始时间,convert(char(5),stime,108) as 结束时间,
(case when (select count(1) from @t where sid>a.sid and convert(char(10),stime,23)=convert(char(10),a.stime,23)
and stime>a.etime and etime<a.etime)>0 then '冲突' else '不冲突' end
)是否冲突
from @t a
/*
sid etime stime 起始时间 结束时间 是否冲突
----------- ----------------------- ----------------------- ----- ----- ------
111 2013-03-07 07:12:00 2013-03-07 08:13:00 07:12 08:13 不冲突
222 2013-03-07 09:00:00 2013-03-07 10:10:00 09:00 10:10 不冲突
333 2013-03-07 11:14:00 2013-03-07 12:11:00 11:14 12:11 不冲突
(3 行受影响)
*/
insert into table1(sid,etime,stime) --插入数据库
select sid,etime,stime from @t
where @time1>=dateadd(d,datediff(d,etime,0),stime) or @time2<=dateadd(d,datediff(d,etime,0),etime)
--生成测试数据
declare @t table(sid int, etime smalldatetime , stime smalldatetime)
insert into @t
select '111', '2013-3-7 7:12:00', '2013-3-7 8:13:00' union all
select '222', '2013-3-7 9:00:00','2013-3-7 10:10:00'union all
select '333', '2013-3-7 11:14:00 ','2013-3-7 12:11:00'
--定义变量
declare @time1 smalldatetime,@time2 smalldatetime
set @time1='08:00' --起始时间
set @time2='09:10' --结束时间
--输出结果
select *,convert(char(5),@time1,108) as 起始时间,convert(char(5),@time2,108) as 结束时间,
case when @time1>=dateadd(d,datediff(d,etime,0),stime) or @time2<=dateadd(d,datediff(d,etime,0),etime) then '时间不冲突' else '时间冲突' end as 是否冲突
from @t
/*
sid etime stime 起始时间 结束时间 是否冲突
----------- --------------------- ---------------------- ----- ----- ----------
111 2013-03-07 07:12:00 2013-03-07 08:13:00 08:00 09:10 时间冲突
222 2013-03-07 09:00:00 2013-03-07 10:10:00 08:00 09:10 时间冲突
333 2013-03-07 11:14:00 2013-03-07 12:11:00 08:00 09:10 时间不冲突
(所影响的行数为 3 行)
*/