我们是五月的花海 , 用青春拥抱时代 |

兴想事成

园龄:12年10个月粉丝:25关注:97

回答博客园友的一个计算题

 

提问原文链接: https://q.cnblogs.com/q/136303/

 

如现在有一张表A,表中有两个字段,开始时间STARTTIME,结束时间ENDTIME
两个时间的值分别为:STARTTIME('2021/8/6 13:25:30'),ENDTIME('2021/8/9 10:12:30)
现在需要计算两个时间差=结束时间-开始时间'
计算结果为=0.865972222,这里去除了两天的周末,时间未满24小时
请问,用SqlServer语句怎么实现这样的效果

 

我给出的解决方案:

1.考虑到都是同一天,并且在周六 或者 周天, 直接返回 0

2.如果是 连续  的周六 周天, 这两者都在休息日 ,直接返回0

3.其他只要有一个不是周六 或者周天的, 进行计算时间差

 

复制代码
 1 -------------------------------------
 2 create function  GetWorkDayDiff(@start datetime, @end datetime) returns decimal(15,8) 
 3 as 
 4 begin 
 5  --sql 中 星期 天为 周的 第一天, 星期 一为 周的第二天,星期6为周的第7天
 6 --SELECT DATEPART(DW,'2021-08-14')  = 7
 7 --SELECT DATEPART(DW,'2021-08-15')  = 1
 8 
 9 
10  if(@start>@end)
11   begin
12     --如果开始时间大于结束时间, 互换一下时间
13     declare @init_time datetime = @start;
14     set @start = @end;
15     set @end = @init_time;
16   end
17 
18 declare @resultDiff  decimal(15,8)=0;
19 declare @dayAllDiff decimal(15,8)=0;
20 select @dayAllDiff =DATEDIFF (ss, @start,@end)/(60*60*24*1.0000);
21 declare @t_day_start datetime =   cast( convert(varchar(10),@start,120) as datetime); --格式化为 yyyy-MM-dd 格式的时间
22 declare @t_day_end datetime =   cast( convert(varchar(10),@end,120) as datetime); --格式化为 yyyy-MM-dd 格式的时间
23 
24     if (@t_day_start=@t_day_end ) and (DATEPART(DW,@t_day_start)=7 or DATEPART(DW,@t_day_start)=1)
25      begin
26        -- print '======================================================================================================'
27        -- print '==========开始时间和结束时间,都在同一天,并且该天是休息日, 返回 0'
28        -- print '======================================================================================================'
29         return @resultDiff;
30      end
31     else if(dateadd(day,1,@t_day_start)= @t_day_end  and DATEPART(DW,@t_day_start)=7)
32      begin
33        -- print '======================================================================================================'
34        -- print '==========开始时间和结束时间是相邻的 周六周天,都是休息日,返回 0'
35        -- print '======================================================================================================'
36         return @resultDiff;
37      end
38 
39 
40     declare @dayOffCount decimal(15,8)=0;
41 
42     while (@t_day_start<@t_day_end)
43     begin
44        set @t_day_start=dateadd(dd,1,@t_day_start);
45        if(DATEPART(DW,@t_day_start)=7 or DATEPART(DW,@t_day_start)=1)
46          begin
47              --print cast(@t_day_start as varchar(10))+' 是休息日'
48             set @dayOffCount = @dayOffCount+1;
49          end
50            --print cast(@t_day_start as varchar(10))+' 是工作日'
51     
52     end
53 
54     --计算两者 时间差 = 总时间差-休息日
55     set @resultDiff = @dayAllDiff-@dayOffCount
56     return @resultDiff;
57 end 
58 -------------------------------------
复制代码

 

测试代码:

declare @start datetime, @end datetime;

set @start = '2021-08-06 13:25:30';
set @end = '2021-08-09 09:12:30';

select dbo.GetWorkDayDiff(@start,@end)

 

posted @   兴想事成  阅读(727)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Good-bye My Loneliness ZARD
  2. 2 Say OK Vanessa Hudgens
  3. 3 All The Love In The World The Corrs
  4. 4 Adesso E Fortuna ~炎と永遠~ 加藤いづみ
All The Love In The World - The Corrs
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

I'm not looking for someone to talk to

I've got my friends, I'm more than O.K.

I've got more than a girl could wish for

I live my dreams but it's not all they say

Still I believe (I'm missing) I'm missing something real

I need someone who really sees me...

(Don't wanna wake...) Don't wanna wake up alone anymore

Still believing you'll walk through my door

All I need is to know it's for sure

Then I'll give... all the love in the world

I've often wondered if love's an illusion

Just to get you through the loneliest days

I can't criticize it

I have no hesitation

My imagination just stole me away

(Still...) Still I believe

(I'm missing) I'm missing something real

I need someone who really sees me...

(Don't wanna wake...) Don't wanna wake up alone anymore (

Still believing you'll walk through my door

All I need is to know it's for sure

Then I'll give... all the love in the world

Love's for a lifetime not for a moment

So how could I throw it away

Yeah I'm only human

And nights grow colder

With no-one to love me that way

Yeah I need someone who really sees me...

(Don't wanna wake...) And i won't wake up alone anymore (

Still believing you'll walk through my door

You'll reach for me and I'll know it's for sure

Then I'll give all the love in the world

(don’t want to wake up alone anymore) alone (

(don’t want to wake up alone) just reach for me (

(don’t want to wake up alone) just don’t want to be alone

(don’t want to wake up alone) don’t want to wake up alone

(don’t want to wake up alone) let me know it’s sure

(don’t want to wake up alone) still believing someone

(don’t want to wake up alone) will reach for me

(don’t want to wake up alone) let me know it’s sure