创建表格假日主人,其名称为假日日期,日期(星期日,星期一或任何公众假期)。
创建一个将返回4个输出列的函数:Date,day of month,year&天。 日期将是2个日期之间的工作日期(忽略假日主表中的日期)
CREATE function DATENEWww
AS
BEGIN
DECLARE @StartDate DATETIME
DECLARE @MIDDLEDATE DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2013/05/01'
SET @EndDate = '2013/05/10'
SELECT getday()'Day', getDATE() 'Today', YEAR(GETDATE()) 'YEAR Part'
SELECT
(DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME (DW, @MIDDLEDATE)='SELECT HOLIDAY_DATE FROM HOLIDAY_MASTER' THEN 1 ELSE 0 END)
END
答案 0 :(得分:0)
使用表格功能:
CREATE FUNCTION [dbo].[CalculateNumberOFWorkDays] (@StartDate datetime, @EndDate datetime)
RETURNS RETURNS @Results TABLE (
[Date] datetime,
[DayOfMonth] int,
[Year] int,
[Day] int
)
AS BEGIN
SET @StartDate = DATEADD(dd, DATEDIFF(dd, 0, @StartDate), 0)
SET @EndDate = DATEADD(dd, DATEDIFF(dd, 0, @EndDate), 0)
DECLARE @WORKDAYS INT
SELECT @WORKDAYS = (DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
INSERT @Results ([Date], [DayOfMonth], [Year], [Day])
SELECT <your query here>
RETURN
END