当前位置:Gxlcms > 数据库问题 > sql 通过某段时间求得改段时间内的工作时长,排除工作日

sql 通过某段时间求得改段时间内的工作时长,排除工作日

时间:2021-07-01 10:21:17 帮助过:14人阅读

CREATE FUNCTION Fun_GetTotalHourBySomeTime(@TaskId NVARCHAR(30),@Bu_trupstartDate NVARCHAR(50),@Bu_trupEndDate NVARCHAR(50))

RETURNS FLOAT
AS
BEGIN
--计算工作日的天数
DECLARE @tStartDate DATE
DECLARE @tEndDate DATE
SET @tStartDate=CONVERT(NVARCHAR(50),@Bu_trupstartDate,112)
SET @tEndDate=CONVERT(NVARCHAR(50),@Bu_trupEndDate,112)

DECLARE @DAYNum DATE ,
@COUNT INT;
SET @DAYNum = @tStartDate;
SET @COUNT = 0;
WHILE @DAYNum <= @tEndDate
BEGIN
SET @COUNT = @COUNT + ( CASE DATEPART(WEEKDAY, @DAYNum)
WHEN 1 THEN 0
WHEN 7 THEN 0
ELSE 1
END );
SET @DAYNum = DATEADD(DAY, 1, @DAYNum);
END
--工作日的天数先少计算一天;
SET @COUNT=@COUNT-1;

DECLARE @sResultHour FLOAT
DECLARE @Day INT
DECLARE @startTimeH INT
DECLARE @endTimeH INT
DECLARE @startTimeM INT
DECLARE @endTimeM INT
--SELECT @sResultHour=8
SELECT @Day=DATEDIFF(DAY,Bu_trupstartDate,Bu_trupEndDate),@startTimeH=startTimeH,@startTimeM=startTimeM,@endTimeH=endTimeH,@endTimeM=endTimeM
FROM dbo.VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
IF(@Day=0)
BEGIN
IF(@startTimeH<12 AND @endTimeH<=12)
BEGIN
SELECT @sResultHour=CAST(DATEDIFF(MINUTE,Bu_trupstartDate,Bu_trupEndDate)*1./60 AS DECIMAL(18,1)) FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
IF(@startTimeH<12 AND @endTimeH>12)
BEGIN
SELECT @sResultHour=(DATEDIFF(HOUR,Bu_trupstartDate,Bu_trupEndDate))-1 FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
IF(@startTimeH>=12)
BEGIN
SELECT @sResultHour=CAST(DATEDIFF(MINUTE,Bu_trupstartDate,Bu_trupEndDate)*1./60 AS DECIMAL(18,1)) FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
END
IF(@Day>=1)
BEGIN
IF(@startTimeH<12 AND @endTimeH<=12)
BEGIN
SELECT @sResultHour=CAST((@COUNT*7.5*60+(@endTimeH-@startTimeH)*60+(@endTimeM-@startTimeM))*1./60 AS DECIMAL(18,1))
FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
IF(@startTimeH<12 AND @endTimeH>12)
BEGIN
SELECT @sResultHour=CAST((@COUNT*7.5*60+(@endTimeH-@startTimeH-1)*60+(@endTimeM-@startTimeM))*1./60 AS DECIMAL(18,1))
FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
IF(@startTimeH>=12)
BEGIN
SELECT @sResultHour=CAST((@COUNT*7.5*60+(@endTimeH-@startTimeH)*60+(@endTimeM-@startTimeM))*1./60 AS DECIMAL(18,1))
FROM VIEW_Apply_Bu_tripReport WHERE taskId=@TaskId
END
END
RETURN @sResultHour
END

如有更好的解决思路,还请分享学习,谢谢!!!

sql 通过某段时间求得改段时间内的工作时长,排除工作日

标签:rtt   res   where   task   view   学习   sele   ima   sre   

人气教程排行