Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Find last working day based on given start date and number of working days

$
0
0
Hello all,I have a business need to come up with a function that calculates the last working day (monday-friday). The reason I need this is for comparison between this year and last years data. The data needs to be calculated based on the same number of working days.Using some of the logic that I found in "http://stackoverflow.com/questions/252519/count-work-days-between-two-dates", I know how many working days we've had this year. Now I need to to use that number to calculate the last working day in the same period last year.I came up with the function below. It works fine as it relates to weekends, starting points and ending points. Where it becomes tricky is that when there is holiday in my holiday table, i want to include that in the logic as well. I can't simply move the end date one day further in case it ends on a holiday. What happens if there are two holidays in a row? Not sure how to incorporate that. Any help is appreciated.USE [master]GO/****** Object: UserDefinedFunction [dbo].[fn_WorkDaysEndDate] Script Date: 11/8/2015 8:05:23 AM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[fn_WorkDaysEndDate] --SELECT [Master].[dbo].[fn_WorkDaysEndDate] ( '11/7/2015', 11) ( @EnteredDate DATETIME ,@WorkingDays INT ) --Define the output data type.RETURNS DATETIMEAS--Calculate the RETURN of the function.BEGIN DECLARE @StartDate DATETIME = @EnteredDate -- in case @EnteredDate starts on Saturday or Sunday, let @WorkingStartDate start on Monday DECLARE @WorkingStartDate DATETIME = ( SELECT DateAdd(day, CASE WHEN DATENAME(WEEKDAY, @EnteredDate) = 'Saturday' THEN 2 WHEN DATENAME(WEEKDAY, @EnteredDate) = 'Sunday' THEN 1 ELSE 0 END, @EnteredDate) ) --End Date = (@WorkingStartDate -1) <<to count the startdate itself>> + Working Days DECLARE @EndDate DATETIME = ( SELECT Dateadd(DAY, @WorkingDays - 1, @WorkingStartDate) ) --Calculate all dates between Start and End Date and store them in @temp SELECT @StartDate = @StartDate DECLARE @temp TABLE (DATE DATETIME) WHILE (@StartDate <= @EndDate) BEGIN INSERT INTO @temp VALUES (@StartDate) SELECT @StartDate = DATEADD(DD, 1, @StartDate) END --SELECT Datename(Month,d.Date) as MonthName, Datename(WEEK,D.date) as WeekNumber, D.Date,Day(D.Date) as Day, --Month(D.Date) as Month, DATENAME(WEEKDAY,D.date) as DateName --FROM @temp D --Find weekend days in temp table, but don't count it if the @EnteredDate is on Saturday or Sunday DECLARE @WeekendDays INT = ( SELECT RealWeekendDays - CASE WHEN Datename(dw, @EnteredDate) = 'Sunday' THEN 1 WHEN DateName(dw, @EnteredDate) = 'Saturday' THEN 2 ELSE 0 END FROM ( SELECT count(*) AS RealWeekendDays FROM @temp WHERE DATENAME(WEEKDAY, DATE) IN ( 'Saturday' ,'Sunday' ) ) D ) --Find public holidays DECLARE @PublicHolidayCount INT = ( SELECT Count(*) FROM [DB].dbo.PublicHolidays WHERE DATE BETWEEN @WorkingStartDate AND @EndDate ) --Add missed days from weekend and public holidays to @EndDate DECLARE @WorkingEndD DATETIME = ( SELECT DateAdd(day, (@WeekendDays + @PublicHolidayCount), @EndDate) ) --In case @WorkinEndD ends on Saturday or Sunday, move it to Monday DECLARE @WorkingEndDate DATETIME = ( SELECT DateAdd(day, CASE WHEN DATENAME(WEEKDAY, @WorkingEndD) = 'Saturday' THEN 2 WHEN DATENAME(WEEKDAY, @WorkingEndD) = 'Sunday' THEN 1 ELSE 0 END, @WorkingEndD) ) --Return outcome RETURN ( SELECT @WorkingEndDate )ENDGO

Viewing all articles
Browse latest Browse all 3145

Trending Articles