16.month_:=floor(months_between(date2,(date1+NUMTOYMINTERVAL(year_, 'year'))));
17.day_ :=date2-((date1+NUMTOYMINTERVAL(year_, 'year'))+NUMTOYMINTERVAL(month_, 'month'));
18.return lpad(year_,4,'0')||'年'||lpad(month_,2,'0')||'月'||lpad(day_,2,'0')||'日'; 19.exception
20. when others then
21. dbms_output.put_line('Input date format exception!'); 22. return ''; 23.end;
测试效果: Sql代码
1. select fn_interval_ymd(sysdate,sysdate-1000) from dual; 2. ----------
3. 0002年08月26日 4. ----------
5. select fn_interval_ymd(to_date('2008-05-01','yyyy-mm-dd'), 6. to_date('2006-04-21','yyyy-mm-dd')) from dual; 7. 0002年00月10日 8. ----------
9. select fn_interval_ymd(to_date('2008-02-11','yyyy-mm-dd'), 10.to_date('2008-03-05','yyyy-mm-dd')) from dual; 11.----------
12.0000年00月23日 DECLARE
CURSOR c IS SELECT * FROM emp ; n number(2); m number(2); d number(2); BEGIN
FOR v IN c LOOP
n:=trunc((sysdate-v.hiredate)/365) ; m:=trunc(((sysdate-v.hiredate)-365*n) /12); d:=trunc(((sysdate-v.hiredate)-365*n) -12*m);
DBMS_OUTPUT.PUT_LINE(v.ename||' '||n||'年'||m||'月'||d||'日'); END LOOP; END; /