时间段冲突的比较

jiangshuaigege 2013-03-07 10:27:42
假如有张数据表register
sid etime stime
111 2013-3-7 7:12:00 2013-3-7 8:13:00
222 2013-3-7 9:00:00 2013-3-7 10:10:00
333 2013-3-7 11:14:00 2013-3-7 12:11:00
... ......... .........
etime,stime是数据库中datetime类型,现在我在asp.net前台网页中有textbox1,和textbox2,分别输入起始时间,与结束时间,如8:00,9:10,即时间段为8:00-9:10(注:只有小时和分钟),现在我通过怎么样的方法实现,与sql数据库中etime-stime时间段的比较(如:数据表第一条记录为:7:12-8:13)数据表中秒全部是零,检验时间段有没有冲突,我是新手请大神指教,最好示例完整的程序。。
...全文
251 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
szm341 2013-03-07
  • 打赏
  • 举报
回复
借用一下ls代码,不知道我理解的对不对


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 行受影响)

*/
asdf147asdf 2013-03-07
  • 打赏
  • 举报
回复
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)
jiangshuaigege 2013-03-07
  • 打赏
  • 举报
回复
引用 2 楼 liuchulong 的回复:
你的意思是界面中文本框中的时间,是要在数据库时间的时间段之间的吗?
我的意思是在前台两个中文框中输入时期+时间,然后截取(时,分)与数据库中stime,etime的时分进行比较,看时间段是不是冲突,不冲突话就插入数据库,用c#写。。
jiangshuaigege 2013-03-07
  • 打赏
  • 举报
回复
引用 4 楼 asdf147asdf 的回复:
SQL code ? 1234567891011121314151617181920212223242526272829 --生成测试数据 declare @t table(sid int, etime smalldatetime , stime smalldatetime) insert into @t select '111', '2013……
你好,数据有多列,我想实现的是如果时间段不冲突就插入数据库,用c#写
jiangshuaigege 2013-03-07
  • 打赏
  • 举报
回复
引用 3 楼 szm341 的回复:
select convert(varchar(5),getdate(),108)获得时分部分
这个我知道,但是怎么比较呢??忘了说了。。前台textbox里面的时间其实还是日期加时间,我只是截取texebox里面的时间(时分,与数据库里面的时分进行比较),如果不重复就插入数据库,重复的话就不插入。
asdf147asdf 2013-03-07
  • 打赏
  • 举报
回复

--生成测试数据
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 行)

*/
szm341 2013-03-07
  • 打赏
  • 举报
回复
select convert(varchar(5),getdate(),108)获得时分部分
liuchulong 2013-03-07
  • 打赏
  • 举报
回复
你的意思是界面中文本框中的时间,是要在数据库时间的时间段之间的吗?
jiangshuaigege 2013-03-07
  • 打赏
  • 举报
回复
顶,坐等回答

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧