日期:2014-05-17  浏览次数:20861 次

oracle 10g 触发器中时间相减问题
oralce 10g 触发器中时间相减

开始时间 格式:2008-12-10
结束时间 格式:2011-11-10 18:54:25

如何用开始时间和结束时间 算出 中间经历的时间。输出格式为:2年11月

下面是触发器:

SQL code


create or replace trigger insert_out_user
  after delete on employee
  for each row
declare
 v_num number;
begin

  select count(*) into v_num from out_employee;

  insert into out_employee
  values
    (
    v_num + 1,
     :old.u_name,
     :old.u_entry_time,
     sysdate,
     此字段就是通过两时间相减得到的值。该怎么写,谢谢各位大神……,     
     :old.u_department,
     :old.u_post,
     :old.u_sex,
     :old.u_age,
     :old.u_phone
     );

  end insert_out_user;







------解决方案--------------------
SQL code
create or replace trigger insert_out_user
  after delete on employee
  for each row
declare
 v_num number;
 v_year varchar2(20);
begin

  select count(*) into v_num from out_employee;

SELECT replace(trunc(months_between(a,b)/12,1),'.','年')||'月' into v_year
FROM
(
SELECT trunc(to_date('2011-11-10 18:54:25','yyyy-mm-dd hh24:mi:ss'),'mm')a,
trunc(to_date('2008-12-10','yyyy-mm-dd'),'mm') b
FROM dual
);
  insert into out_employee
  values
    (
    v_num + 1,
     :old.u_name,
     :old.u_entry_time,
     v_year;
     :old.u_department,
     :old.u_post,
     :old.u_sex,
     :old.u_age,
     :old.u_phone
     );

  end insert_out_user;