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

如何判断一个时间,是否在两个字段的时间段内?
RT
有个假期表,里面有开始时间,结束时间
然后传来一个时间,如05-01。
判断这个时间在不在表中的时间段内

------解决方案--------------------
时间 between 字段1 and 字段2
------解决方案--------------------
如果你不想包含字段1和字段2
可以
where 时间>字段1 and 时间<字段2
------解决方案--------------------
where 05-01>开始时间
And 05-01<结束时间
??
------解决方案--------------------
@dt between right(convert(varchar(10), 开始时间,120),5) and right(convert(varchar(10), 结束时间,120),5)
------解决方案--------------------
select *,case when 时间 between 开始时间 and 结束时间 then '在内' else '不在内' end from 假期表
------解决方案--------------------
上面的语句,直接就根据你的假期表, 查询出想要的表结果
------解决方案--------------------
SQL code


create table t_time(
studentId int, 
begin_time datetime,
end_time datetime
)

truncate table t_time

insert t_time
select 1,'2010-4-6', '2010-5-6'
union all
select 2,'2010-4-5', '2010-4-6'
union all
select 3,'2010-4-6', '2010-4-7'
union all
select 4,'2010-4-5', '2010-4-9'
union all
select 5,'2010-5-1', '2010-5-10'
union all
select 6,'2010-5-5', '2010-5-6'


select * from t_time
where begin_time <= '2010-5-1' and end_time >'2010-5-1'

------解决方案--------------------
学习了
------解决方案--------------------
SQL code

use PracticeDB
go
if exists (select 1 from sysobjects where name='tb')
drop table tb
go
create table tb (id int,start_time date,end_time date)
go
insert into tb 
select 1,'2010-01-01','2010-01-03' union all 
select 2,'2010-04-03','2010-04-06' union all
select 3,'2010-05-01','2010-05-03' union all
select 4,'2010-06-13','2010-06-16'

declare @date varchar(10)
set  @date ='05-01'
select id,(case  when @date between right(convert(varchar(10),start_time,120),len(convert(varchar(10),start_time,120))-5) 
                            and right(convert(varchar(10),end_time,120),len(convert(varchar(10),end_time,120))-5) then '在' else '不在' end) [在不在内]
from tb

------解决方案--------------------
我帮楼主验证过了
探讨
SQL code

use PracticeDB
go
if exists (select 1 from sysobjects where name='tb')
drop table tb
go
create table tb (id int,start_time date,end_time date)
go
insert into tb
select 1,'2010-01-……