如何在PL / SQL中添加分钟到timestamp变量?

时间:2016-03-28 22:00:35

标签: plsql timestamp

我正在尝试将30分钟添加到PL / SQL中的时间戳变量,如下所示:

DECLARE
    l_start_timestamp timestamp;
    l_end_timestamp timestamp;
    rnk number;
     BEGIN
            l_start_timestamp := to_timestamp('&start_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
            l_end_timestamp := to_timestamp('&end_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
     .........................
    ..........................
    END;
    /

获得以下错误:

Enter value for start_time: 25-03-2016 00:00:00
old  23:        l_start_timestamp := to_timestamp('&start_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
new  23:        l_start_timestamp := to_timestamp('25-03-2016 00:00:00', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
Enter value for end_time: 26-03-2016 00:00:00
old  24:        l_end_timestamp := to_timestamp('&end_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
new  24:        l_end_timestamp := to_timestamp('26-03-2016 00:00:00', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
        l_start_timestamp := to_timestamp('25-03-2016 00:00:00', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '00:30' HOUR TO MINUTE;
                                                                                            *
ERROR at line 23:
ORA-06550: line 23, column 86:
PLS-00166: bad format for date, time, timestamp or interval literal

请帮助我如何做到这一点。

先谢谢。!

2 个答案:

答案 0 :(得分:3)

尝试添加分钟,而不是将小时数转换为分钟。

DECLARE
  l_start_timestamp timestamp;
  l_end_timestamp timestamp;
  rnk number;
BEGIN
  l_start_timestamp := to_timestamp('&start_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '30' MINUTE;
  l_end_timestamp := to_timestamp('&end_time', 'dd-mm-yyyy hh24:mi:ss') + INTERVAL '30' MINUTE;
END;

答案 1 :(得分:0)

我认为你遇到了一个Oracle错误(至少在11gR2中)。

interval语句完全有效:

SQL> select date'2016-03-29' + interval '01:30' hour to minute from dual;

DATE'2016-03-29'+IN
-------------------
2016-03-29 01:30:00

但是它在PL / SQL上下文事件中不起作用:

declare
  v_date date := date'2016-03-29' + interval '01:30' hour to minute;
begin
  dbms_output.put_line(v_date);
end;
/

ERROR at line 2:
ORA-06550: line 2, column 37:
PLS-00166: bad format for date, time, timestamp or interval literal

可能的解决方法:

declare
  v_date date := date'2016-03-29' + interval '1' hour + interval '30' minute;
begin
  dbms_output.put_line(v_date);
end;
/

2016-03-29 01:30:00

declare
  v_date date;
begin
  select date'2016-03-29' + interval '01:30' hour to minute
    into v_date
    from dual;

  dbms_output.put_line(v_date);
end;
/

2016-03-29 01:30:00