Formula
WITH DateSequence AS (
SELECT CAST(GETDATE() AS DATE) AS BusinessDate
UNION ALL
SELECT DATEADD(DAY, -1, BusinessDate)
FROM DateSequence
WHERE DATEADD(DAY, -1, BusinessDate) > DATEADD(DAY, -14, GETDATE()) -- Generate enough days to cover weekends
),
BusinessDays AS (
SELECT BusinessDate
FROM DateSequence
WHERE DATENAME(WEEKDAY, BusinessDate) NOT IN ('Saturday', 'Sunday')
)
SELECT MIN(BusinessDate) AS TMinus5BusinessDay
FROM (
SELECT BusinessDate, ROW_NUMBER() OVER (ORDER BY BusinessDate DESC) AS RowNum
FROM BusinessDays
) AS NumberedBusinessDays
WHERE RowNum = 5
OPTION (MAXRECURSION 100);
Comments
Post a Comment