Create or replace function f_format_date (v_date varchar2, format varchar)/* time format processing format */return dateas v_enddate date; v_rn varchar2 (2); v_last_date varchar2 (10 ); Y varchar2 (4); m varchar2 (4); D varchar2 (4); H varchar2 (4); Mi varchar2 (2); s varchar2 (2); m_count number; str_date varchar2 (20); begin str_date: = v_date; select to_char (str_date) into v_enddate from dual; return v_enddate; Exception when others then begin if v_date is null then return ''; end if; If length (TRIM (v_date) <4 then return ''; end if; If length (str_date) <14 then str_date: = rpad (TRIM (v_date ), 14, '0'); elsif length (str_date)> 14 then str_date: = substr (str_date, 1, 14); end if; Y: = substr (str_date, 1, 4); m: = substr (str_date, 5, 2); D: = substr (str_date, 7, 2); H: = substr (str_date, 9, 2); MI: = substr (str_date, 11, 2); s: = substr (str_date, 13, 2); select lengthb (m) into m_count from dual; If m_count> 2 then select to_date (str_date) into v_enddate from dual; return v_enddate; end if; -- Obtain the select decode (to_char (last_day (trunc (to_date (substr (str_date, 0101) | '20170101', 'yyyy-mm-dd') of the current year '), 'y') + 31), 'dd'), '29', 'rn ', 'pn') into v_rn from dual; If to_number (m)> 12 then M: = '12'; elsif to_number (m) <1 then M: = '01'; end if; If to_number (d) <1 then D: = '01 '; elsif to_number (d)> 28 then -- Obtain select to_char (last_day (to_date (Y | M | '01 ', 'yyyy-mm-dd'), 'yyyymmdd') into v_last_date from dual; If to_number (substr (v_last_date, 7,2) <to_number (d) then D: = substr (v_last_date, 7,2); end if; If to_number (h)> 23 then h: = '23'; elsif to_number (h) <1 then h: = '01'; end if; If to_number (MI)> 59 then h: = '59'; elsif to_number (MI) <0 then MI: = '00 '; end if; If to_number (s)> 59 then s: = '59'; elsif to_number (s) <0 then s: = '00'; end if; str_date: = Y | M | d | H | mi | S; -- dbms_output.put_line (str_date); select to_date (str_date, 'yyyymmddhh24miss ') into v_enddate from dual; return v_enddate; Exception when others then return ''; end;
example: to_date (XXX, 'yyyy-mm-dd')