Count Business Days between Two Dates in SQL

Create Table BusinessCalendar
 (BDate smalldatetime Primary Key, BType tinyint)
Go

 –Load dates into table:
–This will handle business dates and weekends but not holidays. Change date range as needed. 

Declare @dt smalldatetime
Set @dt=’02/28/2010′
While @dt<=’03/13/2010′
 Begin
  Insert BusinessCalendar Values
   (@dt,
    Case
     When datepart(dw,@dt) Between 2 and 6
      Then 1 Else 0
    End)
  SET @dt = @dt + 1
 End

Declare @startdate smalldatetime, @enddate smalldatetime
Set @startdate=’02/28/2010′
Set @enddate=’03/13/2010′ 

Select BDays=Count(*)
From BusinessCalendar
Where BType=1
And BDate Between @startdate And @enddate

drop table BusinessCalendar

Leave a comment