Skip to content

Instantly share code, notes, and snippets.

@mburbea
Last active May 17, 2019 19:16
Show Gist options
  • Save mburbea/e4724d57e8cb17de7c117ee161d27310 to your computer and use it in GitHub Desktop.
Save mburbea/e4724d57e8cb17de7c117ee161d27310 to your computer and use it in GitHub Desktop.
create function fn_workday(@start datetime,@daysToAdd int)
returns table
with schemabinding as
return select workday=dateadd(d,@daysToAdd + ((abs(@daysToAdd)+d-2)/5)*2*sign(@daysToAdd)-d/7,@start)
from (select d= 4 - sign(@daysToAdd)*(4-datepart(dw,@start))) x;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment