I'm creating a query which will adjust the date excluding holidays and weekends.
Example data:
Adjusted Date | Adjusted Date(Excluding Holidays and weekends)
02/06/16 | 02/09/16
On my example, The date is a weekend and adjusted date becomes Feb 9 because Feb 8 is a holiday, so it needs to adjust so that the adjusted date would be a working day. Currently, I have a separated table of all the weekends and holidays in a fiscal year.
select case when (
select count(dbo.WeekendsHoliday.[Weekends & Holidays])
from dbo.WeekendsHoliday
where dbo.WeekendsHoliday.[Weekends & Holidays]
= case when convert(time, [Time Received]) > convert(time, '5:00:00 PM')
then dateadd(day, 1, [Date Received])
else [Date Received]
end
) > 0
then case (datename(DW,
case when convert(time, [Time Received]) > convert(time, '5:00:00 PM')
then dateadd(day, 1, [Date Received])
else [Date Received]
end))
when 'Saturday'
then dateadd(day, 2,
case when convert(time, [Time Received]) > convert(time, '5:00:00 PM')
then dateadd(day, 1, [Date Received])
else [Date Received]
end)
else dateadd(day, 1,
case when convert(time, [Time Received]) > convert(time, '5:00:00 PM')
then dateadd(day, 1, [Date Received])
else [Date Received]
end)
end
end as [Adjusted Date Excluding holidays and weekends]
What happens here is if the holiday is 2 consecutive days (Thursday and Friday), adjusted date would be Saturday which is still not valid because it's a weekend.
Adjusted date is an alias here
CASE WHEN (SELECT count([dbo].[WeekendsHoliday].[Weekends & Holidays]) FROM [dbo].[WeekendsHoliday] WHERE [dbo].[WeekendsHoliday].[Weekends & Holidays] = AdjustedDate > 0 THEN CASE (DATENAME(DW, AdjustedDate)) WHEN 'Saturday' THEN DATEADD(DAY, 2, AdjustedDate) ELSE DATEADD(DAY, 1, AdjustedDate) END END AS [Adjusted Date Excluding holidays and weekends]
– christina G