我正在尝试创建一个函数,我可以计算给定时间段内的总工作时间。
说明: 输入单位为mm / dd / yyyy hh:mm格式。
例如用户输入: startDateTime:01/10/2013 9:30 am endDateTime:12/12/2013 5pm
每日工作时间为早上7点至下午5点。例如,如果某人在上午9点开始工作并在晚上8点结束,那么工作时间将计算为8小时(仅计算上午7点至下午5点之间的工作时间)。
我有假期清单:
每周的周六和周日都是假期 和贝娄是公众假期
假期列表:
7月4日/ YYYY
25分之12/ YYYY
31分之12/ yyyy的
阵亡将士纪念日: - 每年五月的最后一个星期一
劳动节: - 每年九月的第一个星期一
感恩节: - 每年11月的第四个星期四
现在我的问题是如何在任何给定的时间段内找出阵亡将士纪念日,劳动节和感恩节。 最终产出将是给定时期内的总工作时数取代假期。
Bellow功能我只用来计算消除周末的工作时间
CREATE FUNCTION [dbo].[WorkTime]
(
@StartDate DATETIME,
@FinishDate DATETIME
)
RETURNS BIGINT
AS
BEGIN
DECLARE @Temp BIGINT
SET @Temp=0
DECLARE @FirstDay DATE
SET @FirstDay = CONVERT(DATE, @StartDate, 112)
DECLARE @LastDay DATE
SET @LastDay = CONVERT(DATE, @FinishDate, 112)
DECLARE @StartTime TIME
SET @StartTime = CONVERT(TIME, @StartDate)
DECLARE @FinishTime TIME
SET @FinishTime = CONVERT(TIME, @FinishDate)
DECLARE @WorkStart TIME
SET @WorkStart = '07:00'
DECLARE @WorkFinish TIME
SET @WorkFinish = '17:00'
DECLARE @DailyWorkTime BIGINT
SET @DailyWorkTime = DATEDIFF(HOUR, @WorkStart, @WorkFinish)
IF (@StartTime<@WorkStart)
BEGIN
SET @StartTime = @WorkStart
END
IF (@FinishTime>@WorkFinish)
BEGIN
SET @FinishTime=@WorkFinish
END
DECLARE @CurrentDate DATE
SET @CurrentDate = @FirstDay
DECLARE @LastDate DATE
SET @LastDate = @LastDay
WHILE(@CurrentDate<=@LastDate)
BEGIN
IF (DATEPART(dw, @CurrentDate)!=1 AND DATEPART(dw, @CurrentDate)!=7)
BEGIN
IF (@CurrentDate!=@FirstDay) AND (@CurrentDate!=@LastDay)
BEGIN
SET @Temp = @Temp + @DailyWorkTime
END
--IF it starts at startdate and it finishes not this date find diff between work finish and start as minutes
ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate!=@LastDay)
BEGIN
SET @Temp = @Temp + DATEDIFF(HOUR, @StartTime, @WorkFinish)
END
ELSE IF (@CurrentDate!=@FirstDay) AND (@CurrentDate=@LastDay)
BEGIN
SET @Temp = @Temp + DATEDIFF(HOUR, @WorkStart, @FinishTime)
END
--IF it starts and finishes in the same date
ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate=@LastDay)
BEGIN
SET @Temp = DATEDIFF(HOUR, @StartTime, @FinishTime)
END
END
SET @CurrentDate = DATEADD(day, 1, @CurrentDate)
END
-- Return the result of the function
IF @Temp<0
BEGIN
SET @Temp=0
END
RETURN @Temp
END
请帮助我了解如何在特定时期内找到感恩节,劳动节和阵亡将士纪念日。