如何比较PL / SQL中的时间戳

时间:2012-01-16 07:19:20

标签: plsql timestamp

我有这样的陈述:

create table times (time_in timestamp, time_out timestamp);
insert into times values(to_timestamp('02-MAY-11 07.57.00.000000 AM'), to_timestamp('02-MAY-11 07.30.00.000000 PM'));
select extract(hour from(time_out-time_in))||':'||extract(minute from(time_out-time_in)) from times;

EXTRACT(HOURFROM(TIME_OUT-TIME_IN))||':'||EXTRACT(MINUTEFROM(TIME_OUT-TIME_IN))   
--------------------------------------------------------------------------------- 
11:33

现在,我想比较上面的结果。例如:IF [result] > [8 hours] THEN ...

怎么做?

1 个答案:

答案 0 :(得分:1)

SELECT *
FROM times
WHERE time_out - time_in > interval '8' hour

在PL / SQL过程中,这将如下所示:

declare
  result interval day to second;
begin

  -- !!! make sure this select returns only one row, or use a cursor !!!
  select time_out - time_in 
     into result
  from times; 

  if (result > interval '8' hour) then
    dbms_output.put_line('greater');
  end if;
end;
/