这有点简单。只是一个单一的选择语句。我将每个步骤分解成它自己的单列,这样你就可以看到它是如何工作的。不过,您只需要最后一列即可计算出小时数。它依赖于语言环境,因为它使用日期名称,但您可以将其翻转为星期几,只要您知道 DATEFIRST 设置为什么。
此外,这不包括假期。您必须创建自己的假期表。我参考了您可以将其链接到最终公式的位置。
只需将开始和结束日期设置为您想要使用的任何内容,然后在您的代码中使用它,执行查找/替换并用您的字段名称替换这些参数。如果您使用的是 SQL Server 2008 或更高版本,则可以通过将打开/关闭时间切换为时间数据类型来简化很多操作。希望这可以帮助!
declare @startDate datetime = '2013-09-05 10:45:00.000',
@endDate datetime = '2013-09-06 08:15:00.000',
@zeroDate datetime = '1900-01-01 00:00:00.000',
@businessOpen datetime = '1900-01-01 08:00:00.000',
@businessClose datetime = '1900-01-01 17:00:00.000',
@hoursOpen int;
select @hoursOpen = datediff(hour, @businessOpen, @businessClose);
select @hoursOpen as hoursOpen
, @endDate - @startDate as actualTimeCallOpen
, datediff(week, @startDate, @endDate) as wholeWeekendsCallOpen
, datediff(day, @startDate, @endDate) as daysCallOpen
, (DATEDIFF(dd, @StartDate, @EndDate)) --get days apart
-(DATEDIFF(wk, @StartDate, @EndDate) * 2) --subtract whole weekends from the date (*2 is for 2 days per weekend)
+(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) --subtract the start date if it started on sunday (thus, partial weekend)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) --subtract the end date if it ends on saturday (again, partial weekend)
as MthruFDaysOpen
, datediff(hour, @startDate, @endDate) as timeHoursCallOpen
, datediff(minute, @businessOpen, convert(datetime, '1900-01-01 ' + convert(varchar(8),@startDate,108))) / 60.0 as hoursOpenBeforeCall
, datediff(minute, convert(datetime, '1900-01-01 ' + convert(varchar(8), @endDate, 108)), @businessClose) / 60.0 as hoursOpenAfterCall
, (@hoursOpen - ((datediff(minute, convert(datetime, '1900-01-01 ' + convert(varchar(8), @endDate, 108)), @businessClose) + datediff(minute, @businessOpen, convert(datetime, '1900-01-01 ' + convert(varchar(8),@startDate,108)))) / 60.0)) as partialHourDay
, ( ((DATEDIFF(dd, @StartDate, @EndDate)) --get days apart,
- (DATEDIFF(wk, @StartDate, @EndDate) * 2) --subtract whole weekends from the date
+ (CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) --subtract the start date if it started on sunday (thus, partial weekend)
- (CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) --subtract the end date if it ends on saturday (again, partial weekend)
--If you have a table with holidays in it, you can subtract the count of holidays from this as well
--test where the holiday is between startdate and end date and the holiday itself isn't a saturday or sunday
) * @hoursOpen) --multiply the whole days open times hours per day, giving us
+ (@hoursOpen --start with hours open
- ( -- then subtract the sum of hours the business was open before and after the call
(datediff(minute, convert(datetime, '1900-01-01 ' + convert(varchar(8), @endDate, 108)), @businessClose) --calculate this different in minutes for greater accuracy
+ datediff(minute, @businessOpen, convert(datetime, '1900-01-01 ' + convert(varchar(8),@startDate,108)))
) / 60.0) --divide by 60 to convert back to hours before subtracting from @hours open
) as businessTimeOpen