我需要一些 PL/SQL 帮助。我正在尝试计算给定任务的截止日期,因为最长时间为 8 个工作小时,并且中午 12 点至下午 1 点的午餐不包括在该时间中。 例如,如果任务在上午 8 点到达,则截止时间为下午 5 点。如果任务在上午 11 点到达,则应在第二天上午 11 点截止。我认为这会相当容易,但我是 PL/SQL 新手并且刚刚开始。我的想法是使用 sysdate 但这是行不通的。请帮助并为没有发布代码而道歉,但我没有得到任何有帮助的东西。
最佳答案
搞乱这个,以下内容对您来说应该是一个很好的起点,请根据需要进行调整。
它需要开始日期和工作小时数,并返回截止日期。请注意,它四舍五入到最接近的小时(因此,如果开始日期/时间是今天下午 3:45,则它使用下午 4 点作为实际开始日期/时间):
create or replace function get_due_date(i_start_date in date default sysdate, i_hours in number default 8)
return date
as
l_dte date;
l_hours number;
begin
if (i_hours < 1 OR i_hours > 24) then
raise_application_error(-20001, 'Hours must be between 1 and 24');
end if;
l_hours := i_hours + 1;
select dte
into l_dte
from
(
select dte, to_char(dte, 'HH24') hr, levl, row_number() over(order by levl) rnum
from (
-- rounding to nearest hour
select round(i_start_date, 'HH24') + ((level-1)/24) as dte, level levl
from dual
connect by level <= 72
)
where to_char(dte, 'HH24') between '08' and '17'
-- skip lunch hour
and NOT(to_char(dte, 'HH24') = '12')
) x
where rnum = l_hours;
return l_dte;
end;
例如:
select sysdate, get_due_date(sysdate, 8) from dual;
输出:
4/10/2015 3:45:21 PM 4/11/2015 3:00:00 PM
关于oracle - 根据开始日期和工作/午餐时间 PL/SQL 计算截止日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29567065/