Oracle時間格式轉換異常處理函式

2021-05-27 05:46:56 字數 2443 閱讀 5006

create or replace function f_format_date(v_date varchar2,format varchar)

/*時間格式處理格式

*/return date

as 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;

--獲取當年是否潤年

select decode(to_char(last_day(trunc(to_date(substr(str_date,1,4)||'0101','yyyy-mm-dd'),'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

--獲取當月的最後一天

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))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;

end;

例如:to_date(***,『yyyy-mm-dd』)

oracle常用的時間格式轉換 一

1 取得當前日期是本月的第幾周 sql select to char sysdate,yyyymmdd w hh24 mi ss from dual to char sysdate,yy 20030327 4 18 16 09 sql select to char sysdate,w from du...

時間格式轉換

一 在mysql中完成 這種方式在mysql查詢語句中轉換,優點是不占用php解析器的解析時間,速度快,缺點是只能用在資料庫查詢中,有侷限性。1.unix時間戳轉換為日期用函式 from unixtime 一般形式 select from unixtime 1156219870 2.日期轉換為uni...

時間格式轉換

dateformat函式語法 g 年代標誌符 y 年m 月 d 日h 時 在上午或下午 1 12 h 時 在一天中 0 23 m 分s 秒 s 毫秒 e 星期 d 一年中的第幾天 f 一月中第幾個星期幾 w 一年中第幾個星期 w 一月中第幾個星期 a 上午 下午 標記符 k 時 在一天中 1 24 ...