set declare @t table(星期 int,开始时间 varchar(20),结束时间 varchar(20),跨日 int) insert into @t values(1,'10:00','03:00',1) insert into @t values(2,'11:00','04:00',1) insert into @t values(3,'12:00','05:00',1) insert into @t values(4,'13:00','06:00',1)SET DATEFIRST 1declare @date datetime set @date = '2008-12-16 01:00'if exists(select 1 from @t where (星期=datepart(dw,@date) or (跨日=1 and 星期=datepart(dw,@date)-1)) and (@date between (case when 星期=datepart(dw,@date) then convert(char(10),@date,120)+' '+开始时间 else convert(char(10),@date-1,120)+' '+开始时间 end) and (case when 星期=datepart(dw,@date) and 跨日=0 then convert(char(10),@date ,120)+' '+结束时间 when 星期=datepart(dw,@date) and 跨日=1 then convert(char(10),@date+1,120)+' '+结束时间 else convert(char(10),@date,120)+' '+结束时间 end))) print '包含' else print '不包含' -- 包含
--> By dobear_0922(小熊) 2008-12-16 15:13:17 --> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] create table [tb]([星期] int,[开始时间] varchar(8),[结束时间] varchar(8),[跨日] int) insert [tb] select 1,'10:00','03:00',1 union all select 2,'11:00','04:00',1 union all select 3,'12:00','05:00',1 union all select 4,'13:00','06:00',1declare @dt datetime set @dt='2008-12-16 01:00' set datefirst 1select * from [tb] where ([星期]=datepart(dw,@dt) and [开始时间]<=right(convert(varchar(15),@dt,120),5) and ([跨日]=1 or [结束时间]>=right(convert(varchar(15),@dt,120),5))) or ([星期]=datepart(dw,@dt)-1 and [跨日]=1 and [结束时间]>=right(convert(varchar(15),@dt,120),5)) /* 星期 开始时间 结束时间 跨日 ----------- -------- -------- ----------- 1 10:00 03:00 1(1 行受影响) */ drop table tb
declare @t table(星期 int,开始时间 varchar(20),结束时间 varchar(20),跨日 int)
insert into @t values(1,'10:00','03:00',1)
insert into @t values(2,'11:00','04:00',1)
insert into @t values(3,'12:00','05:00',1)
insert into @t values(4,'13:00','06:00',1)SET DATEFIRST 1declare @date datetime
set @date = '2008-12-16 01:00'if exists(select
1
from
@t
where
(星期=datepart(dw,@date) or (跨日=1 and 星期=datepart(dw,@date)-1))
and
(@date
between
(case when 星期=datepart(dw,@date) then convert(char(10),@date,120)+' '+开始时间
else convert(char(10),@date-1,120)+' '+开始时间
end)
and
(case when 星期=datepart(dw,@date) and 跨日=0 then convert(char(10),@date ,120)+' '+结束时间
when 星期=datepart(dw,@date) and 跨日=1 then convert(char(10),@date+1,120)+' '+结束时间
else convert(char(10),@date,120)+' '+结束时间
end)))
print '包含'
else
print '不包含'
--
包含
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([星期] int,[开始时间] varchar(8),[结束时间] varchar(8),[跨日] int)
insert [tb]
select 1,'10:00','03:00',1 union all
select 2,'11:00','04:00',1 union all
select 3,'12:00','05:00',1 union all
select 4,'13:00','06:00',1declare @dt datetime
set @dt='2008-12-16 01:00'
set datefirst 1select * from [tb]
where ([星期]=datepart(dw,@dt) and [开始时间]<=right(convert(varchar(15),@dt,120),5)
and ([跨日]=1 or [结束时间]>=right(convert(varchar(15),@dt,120),5)))
or ([星期]=datepart(dw,@dt)-1 and [跨日]=1 and [结束时间]>=right(convert(varchar(15),@dt,120),5))
/*
星期 开始时间 结束时间 跨日
----------- -------- -------- -----------
1 10:00 03:00 1(1 行受影响)
*/
drop table tb