Oracle學習筆記_10_判斷是否為日期類型


 

 

 

FUNCTION isdate (datestr VARCHAR2, format VARCHAR2) RETURN number IS
    p_date   DATE;
BEGIN
    SELECT TO_DATE (datestr, format)
    INTO p_date
    FROM DUAL;
    RETURN 1;
EXCEPTION
    WHEN OTHERS  THEN
       RETURN 0;
END;

 

 

 

 

多條件模糊查詢時:

function get_date_str ( p_date varchar2) return varchar2 is
     v_date   date;
  begin

     if ( length(p_date) = 4 ) then
         select to_date (p_date, 'yyyy')
         into v_date
         from dual;
         return to_char( v_date ,'yy');
           
     elsif ( length(p_date) = 6 )then
         select to_date (p_date, 'yyyymm')
         into v_date
         from dual;
         return to_char( v_date ,'mm')  || '月-' ||  to_char( v_date,'yy');
         
     elsif ( length(p_date) = 7 )then
         select to_date (p_date, 'yyyy-mm')
         into v_date
         from dual;
         return to_char( v_date ,'mm')  || '月-' ||  to_char( v_date,'yy');             
         
     elsif ( length(p_date) = 8 ) then
         select to_date (p_date, 'yyyymmdd')
         into v_date
         from dual;
         return  to_char(v_date,'dd') || '-' || to_char( v_date,'mm')  || '月-' ||  to_char(v_date,'yy');

     elsif ( length(p_date) = 10 ) then
         select to_date (p_date, 'yyyy-mm-dd')
         into v_date
         from dual;
         return  to_char(v_date,'dd') || '-' || to_char( v_date,'mm')  || '月-' ||  to_char(v_date,'yy');
        
     end if;
     
     return '11-00月-00';
     
  exception
    when others then
       return '00-00月-00';
  end get_date_str;
View Code

 

 

附錄:參考資料

1.oracle中判斷是否為日期/number格式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM