tsql - Return number of working days between 2 dates minus weekend and bank holidays -


hi trying script tsql function return number of working days between 2 dates minus weekend days , uk public holidays, here have far (due lack of experience there may syntax errors etc, appreciate corrections):

create function dbo.fn_workdays (@startdate datetime, @enddate datetime)  --define output data type. returns int --calculate return of function. begin      return (select         (datediff(dd,@startdate, @enddate)+1)-(datediff(wk,@startdate, @enddate)*2)-(case when datename(dw, @startdate) = 'sunday'then 1 else 0 end)-(case when datename(dw, @enddate) = 'saturday' 1 else 0 end)         )end go 

now have access table (or can copy if need be) stored uk bank holidays here screeny of setup:

sceeny of table setup

im new tsql , struggling way through list of bankhols , remove them returning int of function , therefore return correct number of working days between 2 dates passed function.

any , advice appreciated (especially if written in easy understand form ;) )

i have been using calendar table things - 1 entry every date, , metadata weekdays, weekends , holidays various contexts. handy lot of tasks. (you can set simple while loop.)

the counting may this:
(calendarweekday filled datepart(weekday... upon generation, results vary region).

select count(calendardate) tblcalendar      calendardate between @startdate , @enddate     , calendarweekday not in (1, 7) , calendarisbankholiday = 0 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -