drublic1019
IS-IT--Management
Well I have been searching the net for sometime and I was unable to find a nice way to find the number of days in a month excluding weekends. But after way tries and code collections I have finally wrote a script to do it. I figured to save people the same headache that I had I'd post it here. It looks big and bad but it always run under one second.
Code:
declare @startdate datetime,
@enddate datetime
set @startdate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,getdate()),0))+1
set @enddate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,getdate())+1,0))
SELECT
DATEDIFF(d, DATEADD(d, CASE WHEN DATEPART(dw, @startdate) = 7 THEN 1 ELSE 0 END, @startdate), DATEADD(d, CASE WHEN DATEPART(dw, @enddate) = 7 THEN 1 ELSE 0 END, @enddate))
- DATEDIFF(wk, DATEADD(d, CASE WHEN DATEPART(dw, @startdate) = 7 THEN 1 ELSE 0 END, @startdate), DATEADD(d, CASE WHEN DATEPART(dw, @enddate) = 7 THEN 1 ELSE 0 END, @enddate))
- DATEDIFF(wk, DATEADD(d, CASE WHEN DATEPART(dw, @startdate) = 6 THEN 1 ELSE 0 END, @startdate), DATEADD(d, CASE WHEN DATEPART(dw, @enddate) = 6 THEN 1 ELSE 0 END, @enddate))+1