AM / PM

时间:2017-06-25 05:00:48

标签: oracle datetime

我有以下存储过程来计算合并语句所需的时间

create or replace procedure ModAuditData(
O_UpdatedCount out int
,O_EndTime out timestamp 
,O_Duration out int)
as


   P_StartTime timestamp(3) WITH LOCAL TIME ZONE;
  -- EndTime timestamp; 

  begin
    P_StartTime:=to_timestamp(to_char(current_timestamp,'DD/MM/YYYY HH:MI:SS'),'DD/MM/YYYY HH:MI:SS');
   -- merge Statment that does UPSERT 
    O_UpdatedCount :=SQL%ROWCOUNT;
    commit;

    O_EndTime:=to_timestamp(to_char(current_timestamp,'DD/MM/YYYY HH:MI:SS'),'DD/MM/YYYY HH:MI:SS');
    begin 
      select extract( second from (O_EndTime-P_StartTime) )
      into O_Duration
      from dual;
          Exception When others then
      O_Duration:=0;
   end;

  end ModAuditData;

问题是

O_EndTime:=to_timestamp(to_char(current_timestamp,'DD/MM/YYYY HH:MI:SS'),'DD/MM/YYYY HH:MI:SS')

完全相反
P_StartTime:=to_timestamp(to_char(current_timestamp,'DD/MM/YYYY HH:MI:SS'),'DD/MM/YYYY HH:MI:SS');

就AM / PM而言

计算开始和结束时间的正确方法是什么

1 个答案:

答案 0 :(得分:1)

CURRENT_TIMESTAMP已经是TIMESTAMP WITH TIME ZONE,没有理由先将其转换为VARCHAR2,然后再转换回TIMESTAMP。

当你将持续时间作为INTEGER返回时,timestamp(3)(提供高达毫秒的精度)没有多大意义,即全秒。

试试这样:

   P_StartTime timestamp(3) WITH TIME ZONE;

  begin
    P_StartTime := current_timestamp;
   -- merge Statment that does UPSERT 
    O_UpdatedCount :=SQL%ROWCOUNT;
    commit;

    O_Duration := EXTRACT(SECOND FROM (current_timestamp - P_StartTime));
   end;

如果是SQL * Plus,请考虑使用TIMING命令。