a(date1,date2)
date1-date2=天數

解决方案 »

  1.   

    SQL> select ((2003-03-09)-(2003-06-09)) from dual;((2003-03-09)-(2003-06-09))
    ---------------------------
                              3
    这样得到的是3,:(
      

  2.   

    12:40:49 SQL> select to_number(to_char(sysdate,'yyyy')) from dual;TO_NUMBER(TO_CHAR(SYSDATE,'YYYY'))
    ----------------------------------
                                  2003实际:270
    12:41:10 SQL> select to_number(to_char(to_date('2003-06-18','yyyy-mm-dd'),'mm'))-1 from dual;TO_NUMBER(TO_CHAR(TO_DATE('2003-06-18','YYYY-MM-DD'),'MM'))-1
    -------------------------------------------------------------
                                                                5实际:480
    12:41:52 SQL> select to_number(to_char(to_date('2003-06-18','yyyy-mm-dd'),'dd'))-1 from dual;TO_NUMBER(TO_CHAR(TO_DATE('2003-06-18','YYYY-MM-DD'),'DD'))-1
    -------------------------------------------------------------
                                                               17实际:100
    12:42:17 SQL>
      

  3.   

    select ((2003-03-09)-(2003-06-09)) from dual;
    不行12:44:25 SQL> select to_date('2003-03-09')-to_date('2003-06-09') from dual;TO_DATE('2003-03-09')-TO_DATE('2003-06-09')
    -------------------------------------------
                                            -92实际:120
    12:44:41 SQL>
      

  4.   

    写个函数,输入date1,date2,
    使用pl/sql,to_char(date1,'yyyymmdd')字符截取,
    输出年、月、日之差。
      

  5.   

    Oracle开发版主beckhambobo以前已写过了。
      

  6.   

    http://expert.csdn.net/Expert/topic/1843/1843455.xml?temp=.1831171
      

  7.   

    CREATE OR REPLACE  FUNCTION "IDG"."DATEDIFF"   ( P_TYPE in 
        varchar2, 
      P_StartDate in Date,
      P_EndDate in Date )
     Return number
     as 
     a number;
     begin
     if P_Type = 'year' then
     a := extract(year from P_EndDate) - extract(year from P_StartDate);
     elsif P_Type = 'yyyy'  then
     a := extract(year from P_EndDate) - extract(year from P_StartDate);
     ELSIF P_Type = 'yy' then
     a := extract(year from P_EndDate) - extract(year from P_StartDate);
     elsif P_Type = 'month' then
     a := Round((round(p_EndDate,'month')-round(p_StartDate,'month'))/30);
     elsif P_Type = 'mm' then
     a := Round((round(p_EndDate,'mm')-round(p_StartDate,'mm'))/30);
     elsif P_Type = 'm' then
     a := Round((round(p_EndDate,'m')-round(p_StartDate,'m'))/30);
     elsif P_Type = 'Day' then
     a := Round(P_EndDate,'day')-round(P_StartDate,'day'); 
     elsif P_Type = 'dd' then
     a := Round(P_EndDate,'dd')-round(P_StartDate,'dd');
     elsif P_type = 'd' then
     a := Round(P_EndDate,'d')-round(P_StartDate,'d');
     end if;
     return a;
     end;
      

  8.   

    这个是我写的一个DateDiff的函数,不知道对不对