NetWorkDays Function in SQL SERVER 2012 -


is there function in sql server 2012 calculates working days?

i have been searching no luck far.

thanks!

no, sql server doesn't have such functions, can use calendar table:

declare @date_start date = '2016-01-01',         @date_end date = '2016-12-31';  cte ( select @date_start [d], 0 level union select dateadd(day,1,[d]), [level] + 1 [level] cte [level] < datediff(day,@date_start,@date_end) ),  holidays ( --table holidays (usa) select * (values ('2016-01-01'), ('2016-01-18'), ('2016-02-15'), ('2016-05-30'), ('2016-07-04'), ('2016-09-05'), ('2016-10-10'), ('2016-11-11'), ('2016-11-24'), ('2016-12-26')) t(d) )  select  c.d,         case when datepart(weekday,c.d) in (1,7) 0 --saturday , sunday, use (6,7) friday,saturday             when h.d not null 0             else 1 end isworking cte c left join holidays h      on c.d=h.d option (maxrecursion 1000);  

it generate table dates in 2016 year , flag - day working or not.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -