我想在MS SQL Server 2008或更高版本中创建一个函数,用于计算两个日期时间之间的业务分钟数。我已经测试了在这里和其他网站上找到的20多个答案,但却找不到一致的工作。公司的开始和结束时间分别为08:30和17:30。我要求2016-10-09 18:35和2016之间的区别:-11-09 9:00。那是9月10日下午6:45和9月11日上午9点。我希望能够回到30分钟,但它们都会返回0.我真的不想逐分钟地检查每一分钟,只是一些基本的SQL“数学”。
答案 0 :(得分:0)
以下SQL可以转换为1个函数或将它们保存为两个单独的函数。就个人而言,我认为将它们分开是更有用的。您将拥有更少的触摸点,并且可以为多个主服务器提供服务。
在Business Minutes功能中,我添加了一个选项/插图来排除周末和/或假期。如果没有必要,只需删除。
日期范围功能用于生成动态日期范围。它比递归cte方法快得多。
Private Sub CommandButton2_Click()
Dim ie As Object
With CreateObject("Shell.Application").Windows
If .Count > 0 Then
' Get IE
Set ie = .Item(.Count - 1)
Else
' Create IE
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
End If
End With
ie.Document.GetElementById("lst-ib").Value = "cheap plastic doors"
Set ie = Nothing
End Sub
返回
Select [dbo].[udf-Business-Minutes]('2016-09-10 18:35','2016-09-11 9:00')
商务纪要功能
30
通用日期范围功能
ALTER Function [dbo].[udf-Business-Minutes] (@D1 Datetime,@D2 Datetime)
Returns int
Begin
Return (
Select Minutes=count(*)
From [dbo].[udf-Range-Date](case when @D1<=@D2 then @D1 else @D2 end,case when @D1<=@D2 then @D2 else @D1 end,'MI',1)
Where Cast(RetVal as time) between '08:31' and '17:30'
and RetVal >case when @D1<=@D2 then @D1 else @D2 end
and DatePart(DW,RetVal) not in (7,1)
and Cast(RetVal as Date) not in (Select Date from (Values
('2016-01-01','New Year''s Day'),
('2016-01-18','Martin Luther King, Jr,'),
('2016-02-15','Washington''s Birthday'),
('2016-03-25','Good Friday'),
('2016-05-30','Memorial Day'),
('2016-07-04','Independence Day'),
('2016-09-05','Labor Day'),
('2016-11-24','Thanksgiving'),
('2016-11-25','Black Friday'),
('2016-12-26','Christmas Day')
) as Holidays (Date,Name)
)
)
End
**编辑 - 做了一个小调整,允许多天改变8:30到8:31以避免重复计算**
CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )
Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
返回540
虽然(跨越两天)
Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-10 19:00')
返回1080
Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-11 19:00')