2个时间 间隔的 工作日 和 工作小时

CREATE FUNCTION dbo.WORKDAYDIFF(@fromDate datetime, @thruDate datetime);    
RETURNS int  
AS   
BEGIN   
declare @result int  
set @fromDate = dateadd(day, case when datepart(weekday, @fromDate); % 7 <= 1 then 2 - datepart(weekday, @fromDate); % 7 else 0 end, @fromDate);   
set @thruDate = dateadd(day, case when datepart(weekday, @thruDate); % 7 <= 1 then -1 - datepart(weekday, @thruDate); % 7 else 0 end, @thruDate);   
set @result = datediff(hour,@fromDate,@thruDate); / 24 - datediff(week,@fromDate,@thruDate); * 2  
if(@result < 0); set @result = 0  
return @result  
END  

你可能感兴趣的:(sql,工作)