下面的函数计算两个日期之间的总时间减去周末,当两个日期相差超过一天时它工作正常,但在一天之内结果是负面的,我不太明白,任何人都可以解决它?
ALTER FUNCTION [dbo].[Gettotalhours]
(
@startdate datetime2,@enddate datetime2
)
RETURNS bigint
AS
BEGIN
declare @dateweekends int
declare @totaltime bigint
Set @Dateweekends =
(DATEDIFF(wk, @StartDate, @EndDate) * 2)
+(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
set @totaltime= DATEDIFF(second,@StartDate,@EndDate)-@dateweekends*24*60*60
return @totaltime
END