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

Popular posts from this blog

401 Unauthorized access Error in SSRS

Migrating SQL Server Reporting Services (SSRS) from a busy server to a dedicated server can provide several benefits

Python code to unprotect the excel sheet with a valid password