• 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