Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

NetWorkDays Expand / Collapse
Author
Message
Posted Wednesday, July 23, 2003 12:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, January 28, 2005 9:50 AM
Points: 19, Visits: 1
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.






Post #14491
Posted Thursday, July 24, 2003 3:03 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, October 27, 2014 3:43 AM
Points: 4,427, Visits: 4,177
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.



Markus Bohse
Post #71856
Posted Friday, September 21, 2012 7:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 7:01 AM
Points: 1, Visits: 39
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
Post #1362656
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse