Skip to content

Instantly share code, notes, and snippets.

@benj2240
Created October 27, 2014 15:31
Show Gist options
  • Select an option

  • Save benj2240/05f12d2063dc2b6b996e to your computer and use it in GitHub Desktop.

Select an option

Save benj2240/05f12d2063dc2b6b996e to your computer and use it in GitHub Desktop.
Weekend Dodging
SELECT ProjectedDate = DATEADD( DAY
,CASE(DATEPART(WEEKDAY, DATEADD(DAY, Margin, BaseDate)))
WHEN 7 THEN -1 -- Go back 1 day from Saturday
WHEN 1 THEN 1 -- Go forward 1 day from Sunday
ELSE 0 -- Weekdays stay as-is
END
,DATEADD(DAY, Margin, BaseDate)
)
FROM BaseDates
JOIN ProjectionMargins
SELECT ProjectedDate = DATEADD( DAY
,Margin + CASE(DATEPART(WEEKDAY, DATEADD(DAY, Margin, BaseDate)))
WHEN 7 THEN -1 -- Go back 1 day from Saturday
WHEN 1 THEN 1 -- Go forward 1 day from Sunday
ELSE 0 -- Weekdays stay as-is
END
,BaseDate
)
FROM BaseDates
JOIN ProjectionMargins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment