I want to create a function in MS SQL server 2008 or higher that calculates the business minutes between two datetimes. I have tested 20+ answers found here and on other sites and cant yet find one that works consistently. The start and and end times of the business are 08:30 and 17:30. I ask for the difference between 2016-10-09 18:35 and 2016:-11-09 9:00. That is Sept 10th 6:45PM and Sept 11th 9AM. I expect to get back 30 minutes, but they all return 0. I really dont want to loop minute by minute checking each minute, just some basic SQL "math".
The following SQL can be converted into 1 function or keep them as two separate. Personally, I think keeping them separate is more useful. You'll have fewer touch-points and can server multiple masters.
In the Business Minutes function, I added an option/illustration to exclude Weekends and/or Holidays. Simply remove if not necessary.
The Date-Range function is used to generate dynamic date ranges. It is much faster than the recursive cte approach.
Select [dbo].[udf-Business-Minutes]('2016-09-10 18:35','2016-09-11 9:00')
Returns
30
The Business Minute Function
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
The Generic Date Range Function
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)
*/
**EDIT - Made a small adjustment to allow for multiple days changed 8:30 to 8:31 to avoid double count **
Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-10 19:00')
Returns 540
While (spanning two days)
Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-11 19:00')
Returns 1080
Select [dbo].[udf-Business-Minutes]('2015-12-31 12:30','2016-01-03 11:30:00') --Returns 300
Select [dbo].[udf-Business-Minutes]('2016-09-30 05:00','2016-09-30 19:00:00') --Returns 540
Select [dbo].[udf-Business-Minutes]('2016-09-29 05:00','2016-09-30 19:00:00') --Returns 1080
Select [dbo].[udf-Business-Minutes]('2015-12-31 12:30','2016-01-03 11:30:00') --Returns 300