I needed a quick and dirty way of getting the last weekday of the month.
ALTER FUNCTION [fn_LastWorkDay](
--Note assumes that Sunday = 1 and Saturday = 7
--get the last day of the month
SET @EOM = DATEADD(hh,-1,DATEADD(mm, DATEDIFF(m,0,@Date )+1, 0))
--get the day of the week
SET @DW = DATEPART(dw,@EOM)
--make sure it is not a weekend day
SELECT @EOM = CASE @DW WHEN 1 THEN DATEADD(d,-2,@EOM)
WHEN 7 THEN DATEADD(d,-1,@EOM)
ELSE @EOM END
I am confident there is a better way using nested datepart functions but this works and is simple to understand.
Started my programming life writing Excel 1.0 macros, God what a long time ago.
Now I'm a dotnet developer, I get to influence direction, play with new toys, build stuff, life is wonderful.
Greatest buzz you can get, walk past a row of desks and see your application running on all of them (and getting paid).
Greatest irritant, pouring 12 months of knowledge and experience into an empty head only to have it leave.