NetWorkDays

  • Has any one created a function/stored procedure that will calculate NetWorkDays like the excel networkdays function?

    From Excel Help file:

    ---------------------

    NETWORKDAYS

    Returns the number of whole working days between start_date and end_date. Working days exclude weekends and any dates identified in holidays. Use NETWORKDAYS to calculate employee benefits that accrue based on the number of days worked during a specific term.

    If this function is not available, run the Setup program to install the Analysis ToolPak. After you install the Analysis ToolPak, you must enable it by using the Add-Ins command on the Tools menu.

    How?

    Syntax

    NETWORKDAYS(start_date,end_date,holidays)

    Start_date is a date that represents the start date. Dates may be entered as text strings within quotation marks (for example, "1/30/1998" or "1998/01/30"), as serial numbers (for example, 35825, which represents January 30, 1998, if you're using the 1900 date system), or as results of other formulas or functions (for example, DATEVALUE("1/30/1998")).

    End_date is a date that represents the end date.

    Holidays is an optional range of one or more dates to exclude from the working calendar, such as state and federal holidays and floating holidays. The list can be either a range of cells that contain the dates or an array constant of the serial numbers that represent the dates. Learn about array constants. For more information about how Microsoft Excel uses serial numbers for dates, see the Remarks section.

  • Though I'm sure there are plenty of other scripts out there check out this article

    http://www.sqlservercentral.com/columnists/achigrik/udfexamples.asp

    It contains several datetime related functions incl. one to calculate workdays.

    [font="Verdana"]Markus Bohse[/font]

  • Hi,

    I trawled the net and found a version by Patrick Jasinski, who I must give the credit too.

    It did't do exactly what I wanted in that it did not handle minus dates so I have altered it to allow for this.

    To answer your question this worked for me.

    CREATE function [dbo].[NetWorkDays](

    @StartDate datetime

    ,@EndDate datetime

    ) returns int as begin

    declare

    @result int

    ,@StartDate2 datetime

    ,@EndDate2 datetime

    ,@DateSwap1 datetime

    ,@DateSwap2 datetime

    ,@ReturnNegative BIT

    set @DateSwap1 = @StartDate

    set @Dateswap2 = @EndDate

    SET @ReturnNegative = 0

    IF @EndDate < @StartDate

    BEGIN

    SET @ReturnNegative = 1

    SET @StartDate = @Dateswap2

    SET @EndDate = @DateSwap1

    END

    set @StartDate2 = dateadd(d,8-datepart(dw, @StartDate), @StartDate)

    set @EndDate2 = dateadd(d,1-datepart(dw ,@EndDate), @EndDate)

    set @result = datediff(d, @StartDate2, @EndDate2) * 5 / 7

    + datediff(d, @StartDate, @StartDate2) - 1

    + datediff(d, @EndDate2, @EndDate)

    - case when datepart(dw,@StartDate) = 1 then 1 else 0 end

    - case when datepart(dw,@EndDate) = 7 then 1 else 0 end

    if @ReturnNegative = 1

    BEGIN

    SET @result = @result * -1

    END

    return @result

    end

    GO

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply