34,590
社区成员
发帖
与我相关
我的任务
分享
create table #tb
(
begintime datetime,
endtime datetime
)
insert into #tb
select '2010-3-22 23:00:00','2010-3-23 7:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 23:00:00','2010-3-23 5:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 5:00:00' union all
select '2010-3-21 23:00:00','2010-3-23 7:00:00'
select * from #tb
select case when day=0 then case when dtend<6 then 0
when dtbegin<6 and dtend>6 then dtend-6
when dtbegin>6 and dtend>6 then dtend-dtbegin end
when day=1 then case when dtend<6 then 24-dtbegin
when dtbegin<6 and dtend>6 then 18+dtend-6
when dtbegin>6 and dtend>6 then dtend-6+(24-dtbegin) end
else case when dtend<6 and dtend<6 then (24-6)*day
when dtend<6 and dtend>6 then (24-6)*day+(dtend-6)
when dtend>6 and dtend>6 then (24-6)*(day-1)+(dtend-6)+(24-dtbegin) end
end as hour
from
(
select *,
datediff(day,begintime,endtime) day,
datepart(hh,begintime) dtbegin,
datepart(hh,endtime) dtend
from #tb
) a
hour
-----------
2
1
1
0
20
(5 行受影响)
create table tb(begintime datetime,endtime datetime)
insert into tb
select '2010-3-22 23:00:00','2010-3-23 7:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 23:00:00','2010-3-23 5:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 5:00:00' union all
select '2010-3-21 23:00:00','2010-3-23 7:00:00'
go
select begintime , endtime,时间差=
case when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) - 1) * (24 - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + (datepart(hh,endtime) - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) -1) * (24 - 6) + (datepart(hh,endtime) - 6)
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + (24 - datepart(hh,begintime))
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime)-1) * (24 - 6) + (24 - datepart(hh,begintime))
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + datepart(hh,endtime) - datepart(hh,begintime)
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) - 1) * (24 - 6) + (datepart(hh,endtime) - 6) + (24 - datepart(hh,begintime))
end
from tb
drop table tb
/*
begintime endtime 时间差
------------------------------------------------------ ------------------------------------------------------ -----------
2010-03-22 23:00:00.000 2010-03-23 07:00:00.000 2
2010-03-23 01:00:00.000 2010-03-23 07:00:00.000 1
2010-03-22 23:00:00.000 2010-03-23 05:00:00.000 1
2010-03-23 01:00:00.000 2010-03-23 05:00:00.000 0
2010-03-21 23:00:00.000 2010-03-23 07:00:00.000 20
(所影响的行数为 5 行)
*/
create table tb(begintime datetime,endtime datetime)
insert into tb
select '2010-3-22 17:00:00','2010-3-23 7:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 23:00:00','2010-3-23 5:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 5:00:00' union all
select '2010-3-21 23:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 1:00:00','2010-3-22 7:00:00'
go
select begintime , endtime,时间差=
case when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) - 1) * (24 - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + (datepart(hh,endtime) - 6)
when datepart(hh,begintime) between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) -1) * (24 - 6) + (datepart(hh,endtime) - 6)
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + (24 - datepart(hh,begintime))
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime)-1) * (24 - 6) + (24 - datepart(hh,begintime))
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) <= datepart(hh,endtime) then datediff(day,begintime,endtime) * (24 - 6) + datepart(hh,endtime) - datepart(hh,begintime)
when datepart(hh,begintime) not between 0 and 6 and datepart(hh,endtime) not between 0 and 6 and datepart(hh,begintime) > datepart(hh,endtime) then (datediff(day,begintime,endtime) - 1) * (24 - 6) + (datepart(hh,endtime) - 6) + (24 - datepart(hh,begintime))
end
from tb
drop table tb
/*
begintime endtime 时间差
------------------------------------------------------ ------------------------------------------------------ -----------
2010-03-22 17:00:00.000 2010-03-23 07:00:00.000 8
2010-03-23 01:00:00.000 2010-03-23 07:00:00.000 1
2010-03-22 23:00:00.000 2010-03-23 05:00:00.000 1
2010-03-23 01:00:00.000 2010-03-23 05:00:00.000 0
2010-03-21 23:00:00.000 2010-03-23 07:00:00.000 20
2010-03-22 01:00:00.000 2010-03-22 07:00:00.000 1
(所影响的行数为 6 行)
*/
select case when datediff(hour,Starttime,Endtime)<0 then 0
when datediff(hour,Starttime,Endtime)>=0 and datediff(hour,Starttime,Endtime)<18 then datediff(hour,Starttime,Endtime)
else datediff(hour,Starttime,Endtime) - (datediff(hour,Starttime,Endtime) + 6)/24*6
end as result
,Starttime1 as Starttime
,Endtime1 as Endtime
from (
select case when datepart(hour,Starttime) >= 0 and datepart(hour,Starttime) <= 6 then dateadd(hour,6-datepart(hour,Starttime),Starttime)
else Starttime end as Starttime
,case when datepart(hour,Endtime) >= 0 and datepart(hour,Endtime) <= 6 then dateadd(hour,-datepart(hour,Endtime),Endtime)
else Endtime end as Endtime
,Starttime as Starttime1
,Endtime as Endtime1
from (
select '2010-01-01 2:00' as Starttime,'2010-01-01 4:00' as Endtime
union all
select '2010-01-01 2:00' as Starttime,'2010-01-01 7:00' as Endtime
union all
select '2010-01-01 2:00' as Starttime,'2010-01-02 4:00' as Endtime
union all
select '2010-01-01 2:00' as Starttime,'2010-01-01 23:00' as Endtime
union all
select '2010-01-01 8:00' as Starttime,'2010-01-01 23:00' as Endtime
union all
select '2010-01-01 8:00' as Starttime,'2010-01-02 5:00' as Endtime
union all
select '2010-01-01 8:00' as Starttime,'2010-01-02 7:00' as Endtime
union all
select '2010-01-01 8:00' as Starttime,'2010-01-02 23:00' as Endtime
) as t
) as t1
result Starttime Endtime
----------- --------------- ----------------
0 2010-01-01 2:00 2010-01-01 4:00
1 2010-01-01 2:00 2010-01-01 7:00
12 2010-01-01 2:00 2010-01-02 4:00
17 2010-01-01 2:00 2010-01-01 23:00
15 2010-01-01 8:00 2010-01-01 23:00
16 2010-01-01 8:00 2010-01-02 5:00
17 2010-01-01 8:00 2010-01-02 7:00
33 2010-01-01 8:00 2010-01-02 23:00
(8 行受影响)
create proc t_sp @begintime datetime,@endtime datetime,@t int output
as
--set @begintime='2010-3-22 23:00:00'
--set @endtime='2010-3-23 7:00:00'
if datediff(day,@begintime,@endtime)>1
BEGIN
select @t=18*(datediff(day,@begintime,@endtime)-1)
select @t=@t+case when @begintime between convert(char(10),@begintime,120) and convert(char(10),@begintime,120)+' 6:00:00' then 18
else datediff(hour,@begintime,dateadd(day,1,convert(char(10),@begintime,120))) end
+
case when @endtime between convert(char(10),@endtime,120) and convert(char(10),@endtime,120)+' 6:00:00' then 0
else datediff(hour,convert(char(10),@endtime,120)+' 6:00:00',@endtime) end
END
if datediff(day,@begintime,@endtime)=1
BEGIN
select @t=case when @begintime between convert(char(10),@begintime,120) and convert(char(10),@begintime,120)+' 6:00:00' then 18
else datediff(hour,@begintime,dateadd(day,1,convert(char(10),@begintime,120))) end
+
case when @endtime between convert(char(10),@endtime,120) and convert(char(10),@endtime,120)+' 6:00:00' then 0
else datediff(hour,convert(char(10),@endtime,120)+' 6:00:00',@endtime) end
END
if datediff(day,@begintime,@endtime)=0
BEGIN
select @t=case when @endtime between convert(char(10),@endtime,120) and convert(char(10),@endtime,120)+' 6:00:00' then 0
else datediff(hour,case when @begintime between convert(char(10),@begintime,120) and convert(char(10),@begintime,120)+' 6:00:00' then convert(char(10),@begintime,120)+' 6:00:00' else @begintime end,@endtime) end
END
RETURN @t
declare @i int
exec t_sp'2010-3-22 23:00:00','2010-3-23 7:00:00',@i output
select @i
exec t_sp'2010-3-23 1:00:00','2010-3-23 7:00:00',@i output
select @i
exec t_sp'2010-3-22 23:00:00','2010-3-23 5:00:00',@i output
select @i
exec t_sp'2010-3-23 1:00:00','2010-3-23 5:00:00',@i output
select @i
exec t_sp'2010-3-21 23:00:0','2010-3-23 7:00:00',@i output
select @i
-----------
2
(1 行受影响)
-----------
1
(1 行受影响)
-----------
1
(1 行受影响)
-----------
0
(1 行受影响)
-----------
20
(1 行受影响)
create table #tb
(
begintime datetime,
endtime datetime
)
insert into #tb
select '2010-3-22 17:00:00','2010-3-23 7:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 23:00:00','2010-3-23 5:00:00' union all
select '2010-3-23 1:00:00','2010-3-23 5:00:00' union all
select '2010-3-21 23:00:00','2010-3-23 7:00:00' union all
select '2010-3-22 1:00:00','2010-3-22 7:00:00'
select * from #tb
select
case
when diff = 1 then 24 - dtbegin + dtend - 6
when diff > 1 then 24 - dtbegin + dtend - 6 + (diff-1)*(24-6)
when dtbegin <= 6 and dtend <= 6 then 0
when dtbegin < 6 and dtend > 6 then dtend - 6
when dtbegin > 6 then dtend - dtbegin
end hour
from
(
select
datediff(day,begintime,endtime) diff,
datepart(hh,begintime) dtbegin,
datepart(hh,endtime) dtend
from #tb
) a
hour
-----------
8
1
0
0
20
1
(6 行受影响)
select datediff(hour,dateadd(hour,6,'2010-3-22 23:00:00'),'2010-3-23 7:00:00')
-----------
2
(1 行受影响)