Count work days between two dates

前端 未结 24 2741
失恋的感觉
失恋的感觉 2020-11-22 01:05

How can I calculate the number of work days between two dates in SQL Server?

Monday to Friday and it must be T-SQL.

24条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 01:44

    Create Function dbo.DateDiff_WeekDays 
    (
    @StartDate  DateTime,
    @EndDate    DateTime
    )
    Returns Int
    As
    
    Begin   
    
    Declare @Result Int = 0
    
    While   @StartDate <= @EndDate
    Begin 
        If DateName(DW, @StartDate) not in ('Saturday','Sunday')
            Begin
                Set @Result = @Result +1
            End
            Set @StartDate = DateAdd(Day, +1, @StartDate)
    End
    
    Return @Result
    

    End

提交回复
热议问题