日期:2014-05-18  浏览次数:20334 次

时间求交集
有表Tab_A里面T字段里面存储值从8:00,8:30,9:00.....一直到18:00 ,值固定。
输入开始时间,结束时间,输入的格式为 0:00...5:30....24:00 。
如何取得输入的开始时间、结束时间和Tab_A表里面T字段比较获得交集。

------解决方案--------------------
SQL code
select  *  from  Tab_A where T >=开始时间 and T<=结束时间

------解决方案--------------------
between ..and ..
------解决方案--------------------
SQL code
where T between 开始时间 and  结束时间

------解决方案--------------------
先获取到开始时间,结束时间
假设你输入的是字符串@s,以逗号区分开始时间,结束时间,例如:'0:00,5:30'

select * from tab_a where 
cast(T as datetime) between case(left(@s , charindex(@s,',') - 1) as datetime) and cast(substring(@s , charindex(@s,',') + 1 , len(@s)) as datetime)