--满周岁
select trunc(months_between(&edate, &sdate)/12) from dual;  --不需要满周岁
select ceil(months_between(&edate, &sdate)/12) from dual;

解决方案 »

  1.   

    create or replace function get_age(bdate in date) return number is
      bd_year    number(4);
      bd_month   number(2);
      bd_day     number(2);
      this_year  number(4);
      this_month number(2);
      this_day   number(2);  Result number;
    begin
      bd_year    := to_char(bdate, 'yyyy');
      bd_month   := to_char(bdate, 'mm');
      bd_day     := to_char(bdate, 'dd');
      this_year  := to_char(sysdate, 'yyyy');
      this_month := to_char(sysdate, 'mm');
      this_day   := to_char(sysdate, 'dd');  result := this_year - bd_year + (this_month - bd_month) / 12 +
                (this_day - bd_day) / 30.4375 / 12;
      return(Result);
    end get_age;
    /
    -- 随便写写,不知道有没有更好的方法。
      

  2.   

    RobinHZ我觉得应该可以用函数去简化这个操作xiaoxiao1984,select trunc(months_between(sysdate, date'2006-05-06')/12) from dual;  为什么需要加DATE才可以执行。我想你是正解
      

  3.   

    不一定需要加date才可以执行,如果你已经设置好了数据库的日期时间格式(alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'),可以
    select trunc(months_between(sysdate, '2006-05-06')/12) from dual;
    直接就可以执行;通用的话,可以
    select trunc(months_between(sysdate, to_date('2006-05-06','yyyy-mm-dd'))/12) from dual;
    对传入的字符串转换一下格式