首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么判断一个时间,是否在两个字段的时间段内

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

如何判断一个时间,是否在两个字段的时间段内?
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_timeinsert t_timeselect 1,'2010-4-6', '2010-5-6'union allselect 2,'2010-4-5', '2010-4-6'union allselect 3,'2010-4-6', '2010-4-7'union allselect 4,'2010-4-5', '2010-4-9'union allselect 5,'2010-5-1', '2010-5-10'union allselect 6,'2010-5-5', '2010-5-6'select * from t_timewhere begin_time <= '2010-5-1' and end_time >'2010-5-1'
[解决办法]
学习了
[解决办法]
SQL code
use PracticeDBgoif exists (select 1 from sysobjects where name='tb')drop table tbgocreate table tb (id int,start_time date,end_time date)goinsert into tb select 1,'2010-01-01','2010-01-03' union all select 2,'2010-04-03','2010-04-06' union allselect 3,'2010-05-01','2010-05-03' union allselect 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-……

热点排行