zoukankan      html  css  js  c++  java
  • 请假两时间的小时数差


    /*
    * 请假两时间的小时数差
    */
    create PROCEDURE [dbo].[os_vocation_hour]
    @starttime varchar(20), --开始时间
    @endtime varchar(20) --结束时间

    AS
    if(isdate(@starttime)=0 or isdate(@endtime)=0) --非日期格式
    begin
    select 0
    end
    else --正常日期格式
    begin
    declare @day_jg varchar(20),@day_num float,@hour_num float,@hour_temps float,@hour_tempe float -- 声明三个变量
    declare @s1 datetime,@e1 datetime,@num_old float

    set @hour_temps=DatePart(Hour,@starttime) --获取开始小时
    set @hour_tempe=DatePart(Hour,@endtime) --获取结束小时

    if(@hour_temps>=8 and @hour_temps<=17 and @hour_tempe>=8 and @hour_tempe<=17) --白班
    begin
    select @hour_num=datediff(hour,cast(@starttime as datetime),cast(@endtime as datetime)) -- 获取两时间的小时数的差
    select @day_num=datediff(day,cast(@starttime as datetime),cast(@endtime as datetime)) -- 获取两时间的天数的差
    if(@day_num=0 and @hour_num>0) --当天
    begin
    --同天同一上午或同一下午
    if((@hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=8 and @hour_tempe<=12) or (@hour_temps>=13 and @hour_temps<=17 and @hour_tempe>=13 and @hour_tempe<=17) )
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))
    else if @hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=13 and @hour_tempe<=17 --开始在上午,结束在下午
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60
    else --时间段不对
    set @day_jg=-1000
    end
    else if(@day_num=1) --隔天
    begin
    if(@hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=8 and @hour_tempe<=12) --开始在上午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15
    end
    else if(@hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=13 and @hour_tempe<=17) --开始在上午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-60
    end
    else if(@hour_temps>=13 and @hour_temps<=17 and @hour_tempe>=8 and @hour_tempe<=12) --开始在下午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60*15
    end
    else if(@hour_temps>=13 and @hour_temps<=17 and @hour_tempe>=13 and @hour_tempe<=17) --开始在下午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15
    end
    end
    else if(@day_num>1) --多天
    begin
    if(@hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=8 and @hour_tempe<=12) --开始在上午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-(@day_num-1)*24*60
    end
    else if(@hour_temps>=8 and @hour_temps<=12 and @hour_tempe>=13 and @hour_tempe<=17) --开始在上午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-60-(@day_num-1)*24*60
    end
    else if(@hour_temps>=13 and @hour_temps<=17 and @hour_tempe>=8 and @hour_tempe<=12) --开始在下午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60*15-(@day_num-1)*24*60
    end
    else if(@hour_temps>=13 and @hour_temps<=17 and @hour_tempe>=13 and @hour_tempe<=17) --开始在下午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-(@day_num-1)*24*60
    end
    --处理小时 --处理天数
    set @num_old=@day_num
    set @s1=@starttime

    while(@s1<=@endtime) --用while循环判断是否周末
    begin
    if(datename(weekday,@s1)='星期六' or datename(weekday,@s1)='星期日') --是周末,则天数减1
    set @day_num=@day_num-1
    set @s1=dateadd(day,1,@s1) --下一天
    end
    set @day_num=@day_num-1 --天数少1
    set @day_jg=@day_jg+@day_num*8*60 --得出分钟数
    end

    end
    else if((@hour_temps>=20 and @hour_temps<=24 or @hour_temps>=0 and @hour_temps<=5) and (@hour_tempe>=20 and @hour_tempe<=24 or @hour_tempe>=0 and @hour_tempe<=5)) --夜班
    begin
    set @starttime=dateadd(Hour,-12,@starttime) --提前12小时
    set @endtime=dateadd(Hour,-12,@endtime)
    select @day_num=datediff(day,@starttime,@endtime) -- 获取两时间的天数的差
    select @hour_num=datediff(hour,cast(@starttime as datetime),cast(@endtime as datetime))
    set @hour_temps=DatePart(Hour,@starttime) --获取开始小时
    set @hour_tempe=DatePart(Hour,@endtime) --获取结束小时

    if(@day_num=0 and @hour_num>0) --当天
    begin
    --同天同一上午或同一下午
    if((@hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=8 and @hour_tempe<=13) or (@hour_temps>=14 and @hour_temps<=17 and @hour_tempe>=14 and @hour_tempe<=17))
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))
    else if @hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=14 and @hour_tempe<=17
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60
    else --时间段不对
    set @day_jg=-1000
    end
    else if(@day_num=1) --隔天
    begin
    if(@hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=8 and @hour_tempe<=13) --开始在上午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15
    end
    else if(@hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=14 and @hour_tempe<=17) --开始在上午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-60
    end
    else if(@hour_temps>=14 and @hour_temps<=17 and @hour_tempe>=8 and @hour_tempe<=13) --开始在下午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60*15
    end
    else if(@hour_temps>=14 and @hour_temps<=17 and @hour_tempe>=14 and @hour_tempe<=17) --开始在下午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15
    end
    end
    else if(@day_num>1) --多天
    begin
    if(@hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=8 and @hour_tempe<=13) --开始在上午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-(@day_num-1)*24*60
    end
    else if(@hour_temps>=8 and @hour_temps<=13 and @hour_tempe>=14 and @hour_tempe<=17) --开始在上午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-60-(@day_num-1)*24*60
    end
    else if(@hour_temps>=14 and @hour_temps<=17 and @hour_tempe>=8 and @hour_tempe<=13) --开始在下午,结束在上午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60*15-(@day_num-1)*24*60
    end
    else if(@hour_temps>=14 and @hour_temps<=17 and @hour_tempe>=14 and @hour_tempe<=17) --开始在下午,结束在下午
    begin
    set @day_jg=datediff(mi,cast(@starttime as datetime),cast(@endtime as datetime))-60-60*15-(@day_num-1)*24*60
    end
    --处理小时 --处理天数

    set @num_old=@day_num
    set @s1=@starttime

    while(@s1<=@endtime) --用while循环判断是否周末
    begin
    if(datename(weekday,@s1)='星期六' or datename(weekday,@s1)='星期日') --是周末
    set @day_num=@day_num-1
    set @s1=dateadd(day,1,@s1) --下一天
    end
    set @day_num=@day_num-1
    set @day_jg=@day_jg+@day_num*8*60
    end
    end
    else --有问题的时间
    begin
    set @day_jg=-1000
    end
    set @day_num=@day_jg/60/8 --从分钟数得出天数
    if(@day_jg>0 and @day_jg<8*60) --不足8小时,算0.5天
    set @day_num=0.5
    else if(@day_jg>8*60 and (@day_jg /60) % 8 !=0) --超过8小时,被8整除,有余的,加0.5天
    set @day_num=@day_num+0.5
    declare @t float
    set @t= cast((cast(@day_jg as decimal(10,3))/60) as decimal(10,1)) --时数
    select case when @t-floor(@t)>0 and @t-(floor(@t)+0.5)<=0 then (floor(@t)+0.5) when @t-(floor(@t)+0.5)>0 then (floor(@t)+1) else floor(@t) end
    end

    每天一进步、一积累,创造自我价值,体现人生逼格,你是自己的赢家!
  • 相关阅读:
    【题解】 保镖 半平面交
    【题解】 CF1492E Almost Fault-Tolerant Database 暴力+复杂度分析
    【题解】 闷声刷大题 带悔贪心+wqs二分
    【题解】 「WC2021」表达式求值 按位+表达式树+树形dp LOJ3463
    EasyNVR及EasyRTC平台使用Go语言项目管理GoVendor和gomod的使用总结
    一天一个开发技巧:如何基于WebRTC建立P2P通信?
    HTML5如何实现直播推流?值得学习一下!
    java后端学习-第一部分java基础:Scanner的基本使用
    java后端学习-第一部分java基础:三元运算符、运算符优先级、标识符、关键字和保留字
    java后端学习-第一部分java基础:赋值运算符
  • 原文地址:https://www.cnblogs.com/chlf/p/4221770.html
Copyright © 2011-2022 走看看