SSIS expression to find previous Friday

前端 未结 1 1235
無奈伤痛
無奈伤痛 2021-01-07 15:34

Using SQL Server 2008, I have built an SSIS variable with dynamic value base on current date. I would like to have its value to be Friday if the current day is Monday, and h

相关标签:
1条回答
  • 2021-01-07 15:48

    Will this work (you can substitute GETDATE() for @date, I just used that to easily test out different dates)

    DECLARE @date DATETIME
    SET @date = '2013-01-14'
    
    SELECT
        PrevFriday = CASE WHEN DATEPART(weekday, @date) <> 2 THEN @date
                          ELSE DATEADD(DAY, -3, @date)
                     END
    

    UPDATE: Here is same, but done in SSIS Variable Expression:

    DATEPART("dw", GETDATE()) != 2?
    GETDATE():
    DATEADD("dw", -3, GETDATE())
    

    UPDATE #2: Here is how to return the previous Friday for ANY date, not just Monday

    SELECT DATEADD(DAY, -1 - (DATEPART(weekday, @date) % 7), @date)
    
    0 讨论(0)
提交回复
热议问题