SELECT * FROM dbo.BussinesDaysToCalendarDays (9, DATEPART(WEEKDAY,GETDATE()),2,6)
-- How many calendar days are needed to complete 9 bussines days starting today if the bussines week is Monday To Friday (2 to 6)
SELECT * FROM dbo.BussinesDaysToCalendarDays (9, DATEPART(WEEKDAY,GETDATE()),2,6)
-- How many calendar days are needed to complete 9 bussines days starting today if the bussines week is Monday To Friday (2 to 6)
ALTER FUNCTION [dbo].[BussinesDaysToCalendarDays] ( @BussinesDays INT, @WeekDayToStart INT, @BussinesWeekStart INT, @BussinesWeekEnd INT ) RETURNS TABLE AS RETURN ( WITH Digits(Digit) AS ( SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ), Numbers(Number) AS ( SELECT Centuries.Digit * 100 + Tents.Digit * 10 + Units.digit FROM Digits Units CROSS JOIN Digits Tents CROSS JOIN Digits Centuries ) -- This is just a tally table from 0 to 999 SELECT CalendarDays = MAX([CalendarDay]) FROM ( SELECT TOP (@BussinesDays) [CalendarDay] = d.Number + 1 , [WeekDay], IsBussinesDay = CASE WHEN [WeekDay] BETWEEN @BussinesWeekStart AND @BussinesWeekEnd THEN 1 ELSE 0 END FROM Numbers d CROSS APPLY (SELECT [WeekDay] = ((@WeekDayToStart -1 + d.Number ) % 7) + 1 ) wd WHERE Number BETWEEN 0 AND 365 AND [WeekDay] BETWEEN @BussinesWeekStart AND @BussinesWeekEnd ORDER BY d.Number ) BussinesDays )