计算两个日期相差的工作天数,过滤了周末双休天数

CREATE FUNCTION [dbo].[gdcfn_GetWorkDay](
@dt_begin datetime,  --计算的开始日期
@dt_end  datetime    --计算的结束日期
)RETURNS int
AS
BEGIN    
    set @dt_begin=convert(varchar(10),@dt_begin,120)
    set @dt_end=convert(varchar(10),@dt_end,120)
    
    declare @dayNum int
    if @dt_begin is null or @dt_begin<@dt_end or @dt_end is null begin set @dayNum=null end
    
    set @dayNum=0
    declare @tempDate datetime
    set @tempDate=dateadd(dd,1,@dt_begin)
    while @tempDate<=@dt_end
    begin
        if datepart(dw,@tempDate) not in(1,7) begin
            set @dayNum=@dayNum+1
        end
        set @tempDate=dateadd(dd,1,@tempDate)
    end
    --如果结束日期是周末,并且天数差是0的时候就算一天
    if @dayNum=0 and datepart(dw,@dt_end) in(1,7) begin
        set @dayNum=@dayNum+1
    end
    return @dayNum
end
   

原文地址:https://www.cnblogs.com/huaan011/p/3446179.html