2012-01-16 42 views
1

比較時間戳我有這樣的說法:
如何在PL/SQL

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
​​

一個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; 
/