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
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)