一個想法是找到的第一個星期一(如果我們說,「週末」是指週六和週日)或之後@ D1和上週一@ D2之前。這是整週的數量。所以整整一週的時間是120(5天* 24小時)。然後,我們將在上個星期一之後添加小時,直到@ d2。 @ d2星期幾的一個簡單例子可以讓你在上週的週末裏照顧週末。同樣,我們會計算@ d1到第一個星期一之間的小時數。
declare @d1 datetime, @d2 datetime
select @d1 = '2017-08-10 19:51:01.000', @d2 = '2017-08-16 17:52:14.000'
declare @StartOfFirstWeekAfterStartDate date = cast(dateadd(day, case datepart(weekday, @d1)
when 1 /* Sunday */ then -6
when 2 /* Monday */ then 0
when 3 /* Tuesday */ then 6
when 4 /* Wendsday */ then 5
when 5 /* Thursday */ then 4
when 6 /* Friday */ then 3
when 7 /* Saturday */ then 2
end, @d1) as date),
@StartOfLastWeekBeforeEndDate date = cast(dateadd(day, case datepart(weekday, @d2)
when 1 /* Sunday */ then -6
when 2 /* Monday */ then 0
when 3 /* Tuesday */ then -1
when 4 /* Wendsday */ then -2
when 5 /* Thursday */ then -3
when 6 /* Friday */ then -4
when 7 /* Saturday */ then -5
end, @d2) as date)
declare @NumberOfWorkdays int = datediff(week, @StartOfFirstWeekAfterStartDate, @StartOfLastWeekBeforeEndDate) * 5
declare @HoursWeekBefore int = case datepart(weekday, @d1)
when 1 /* Sunday */ then 0
when 2 /* Monday */ then 5 * 24 - datepart(hour, @d1)
when 3 /* Tuesday */ then 4 * 24 - datepart(hour, @d1)
when 4 /* Wendsday */ then 3 * 24 - datepart(hour, @d1)
when 5 /* Thursday */ then 2 * 24 - datepart(hour, @d1)
when 6 /* Friday */ then 1 * 24 - datepart(hour, @d1)
when 7 /* Saturday */ then 0
end
declare @HoursWeekAfter int = case datepart(weekday, @d2)
when 1 /* Sunday */ then 5 * 24
when 2 /* Monday */ then 0 * 24 + datepart(hour, @d2)
when 3 /* Tuesday */ then 1 * 24 + datepart(hour, @d2)
when 4 /* Wendsday */ then 2 * 24 + datepart(hour, @d2)
when 5 /* Thursday */ then 3 * 24 + datepart(hour, @d2)
when 6 /* Friday */ then 4 * 24 + datepart(hour, @d2)
when 7 /* Saturday */ then 5 * 24
end
declare @HoursDiff int = @HoursWeekBefore + @NumberOfWorkdays * 24 + @HoursWeekAfter
select
@StartOfFirstWeekAfterStartDate,
datepart(WEEKDAY, @StartOfFirstWeekAfterStartDate), -- Should always be Monday
@StartOfLastWeekBeforeEndDate,
datepart(WEEKDAY, @StartOfLastWeekBeforeEndDate), -- Should always be Monday
@NumberOfWorkdays,
datediff(week, @StartOfFirstWeekAfterStartDate, @StartOfLastWeekBeforeEndDate),
@HoursWeekBefore,
@HoursWeekAfter,
@HoursDiff
上面的代碼適用於sp/udf的使用。如果我們把所有這些計算在一個表達式,它會成爲這樣:
declare @d1 datetime, @d2 datetime
select @d1 = '2017-08-10 19:51:01.000', @d2 = '2017-08-16 17:52:14.000'
select case datepart(weekday, @d1)
when 1 /* Sunday */ then 0
when 2 /* Monday */ then 5 * 24 - datepart(hour, @d1)
when 3 /* Tuesday */ then 4 * 24 - datepart(hour, @d1)
when 4 /* Wendsday */ then 3 * 24 - datepart(hour, @d1)
when 5 /* Thursday */ then 2 * 24 - datepart(hour, @d1)
when 6 /* Friday */ then 1 * 24 - datepart(hour, @d1)
when 7 /* Saturday */ then 0
end /* @HoursWeekBefore */
+
datediff(week,
cast(dateadd(day, case datepart(weekday, @d1)
when 1 /* Sunday */ then -6
when 2 /* Monday */ then 0
when 3 /* Tuesday */ then 6
when 4 /* Wendsday */ then 5
when 5 /* Thursday */ then 4
when 6 /* Friday */ then 3
when 7 /* Saturday */ then 2
end, @d1) as date) /* @StartOfFirstWeekAfterStartDate */,
cast(dateadd(day, case datepart(weekday, @d2)
when 1 /* Sunday */ then -6
when 2 /* Monday */ then 0
when 3 /* Tuesday */ then -1
when 4 /* Wendsday */ then -2
when 5 /* Thursday */ then -3
when 6 /* Friday */ then -4
when 7 /* Saturday */ then -5
end, @d2) as date) /* @StartOfLastWeekBeforeEndDate */) * 5 /* @NumberOfWorkdays */ * 24
+
case datepart(weekday, @d2)
when 1 /* Sunday */ then 5 * 24
when 2 /* Monday */ then 0 * 24 + datepart(hour, @d2)
when 3 /* Tuesday */ then 1 * 24 + datepart(hour, @d2)
when 4 /* Wendsday */ then 2 * 24 + datepart(hour, @d2)
when 5 /* Thursday */ then 3 * 24 + datepart(hour, @d2)
when 6 /* Friday */ then 4 * 24 + datepart(hour, @d2)
when 7 /* Saturday */ then 5 * 24
end /* @HoursWeekAfter */
請,不就是上面的計算取決於DATEFIRST設置。我假設星期日是一週的第一天(即datepart(weekday,@Moday)將返回2)。如果不是這種情況,則應調整案例陳述中的偏移量。 我也沒有在這些時間處理會議紀要,所以小時計算有點粗糙。如果這是一個問題,你可以使用上面的方法來計算@ D1和D2 @和60
「週末」,你真的是指「非工作日」嗎?因爲這些不是一回事。而且由於自然災害引發了新聞,所以即使是「標準」工作日也可能實際上並未「起作用」。你如何解決這個問題?在互聯網上搜索「日曆表」。 – SMor