-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate fn_WorkDays.sql
43 lines (37 loc) · 1.22 KB
/
Create fn_WorkDays.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
USE [dB NAME]
GO
IF EXISTS
(
SELECT *
FROM dbo.SYSOBJECTS
WHERE ID = OBJECT_ID(N'[dbo].[fn_NbrWorkDays]')
AND XType IN (N'FN', N'IF', N'TF')
)
DROP FUNCTION [dbo].[fn_NbrWorkDays]
GO
CREATE FUNCTION [dbo].[fn_NbrWorkDays]
--Presets
(
@StartDate DATE,
@EndDate DATE = NULL --@EndDate replaced by Curent Date when DEFAULTed
)
--Define the output data type.
RETURNS INT
AS
--Calculate the RETURN of the function.
BEGIN
--If the Start Date is null, return a NULL and exit.
IF @StartDate IS NULL
RETURN NULL
--If the End Date is null, populate with Current Date value so will have two dates (required by DATEDIFF below).
IF @EndDate IS NULL
SELECT @EndDate = GetDate()
RETURN (
SELECT DATEDIFF(Day, @StartDate, @EndDate) -- Total Days
- (DATEDIFF(Day, 0, @EndDate)/7 - DATEDIFF(Day, 0, @StartDate)/7) -- subtract Sundays
- (DATEDIFF(Day, -1, @EndDate)/7 - DATEDIFF(Day, -1, @StartDate)/7) -- subtract Saturdays
- (Select Count(*) from [KP].[dbo].[Special_Dates] where [ObservedDate] between @StartDate and @EndDate ) -- subtract Holidays
+ (CASE WHEN DATEPART(dw, @EndDate) IN (2,3,4,5,6) THEN 1 ELSE 0 END ) -- add last day if weekday
)
END
GO