2006-08-31

Working Days in T-SQL

These two posts helped greatly in getting a function to work out the working days between two working days:
http://aaronjohals.blogspot.com/2005/10/number-of-working-days-between-2-dates.html
http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-count-the-number-of-business-days-between-two-dates.html


drop function dbo.GetWorkingDaysDiff
go
create function dbo.GetWorkingDaysDiff(@start datetime, @end datetime)
    returns int
as 
begin
    declare @countdays int
    set @countdays = 0
    while @start < @end
    begin
        if (select datepart(dw, @start)) < 6
        begin
            set @countdays = @countdays + 1
        end
        set @start = dateadd(d,1,@start)
    end
    return @countdays
end 
GO
print(dbo.GetWorkingDaysDiff('2006/08/21', '2006/08/27'))
go