有张表A,某个字段为varchar型。他的数据有比如1999-10-22,1999/11/22,1999,1999.12.22,1999.12等。我要把这些数据导到新表,但那字段变为date型。有谁能转换下格式然后插入新表谢谢!!!!

解决方案 »

  1.   

    create or replace function date_convert(f_in in varchar2)return varchar2
    as
    v_year varchar2(4);
    v_month varchar2(2);
    v_day varchar2(2);
    v_dot1 number;
    v_dot2 number;
    v varchar(2);
    l number;
    begin
    l:=length(f_in);
    for i in 1..(case when l>4 then 4 else l end) loop
      v_dot1:=i;
      v:=substr(f_in,i,1);
      if v not between '0' and '9' then
        exit;
      else v_year:=v_year||v;
      end if; 
    end loop;
    if v_dot1=l 
       then return v_year||'-1-1';
    end if;
       
    for i in v_dot1+1..l loop
      v:=substr(f_in,i,1);
      v_dot2:=i;
      if v not between '0' and '9' then 
        if v_month is null then goto block1;
        else exit; 
        end if;
      elsif v >'1' or length(v_month)=2 then v_month:=v_month||v; exit;
      else v_month:=v_month||v;
      end if;
       <<block1>>
    null;
    end loop;
    if v_dot2=l or v_month is null
       then return v_year||'-'||nvl(v_month,'1')||'-1';
    end if;for i in v_dot2+1..l loop
      v:=substr(f_in,i,1);
      if v not between '0' and '9' then 
        if v_day is null then goto block2;
        else exit; 
        end if;
      elsif v >'3' then v_day:=v_day||v; exit;
      else v_day:=v_day||v;
      end if;
       <<block2>>
    null;
    end loop; 
    return v_year||'-'||v_month||'-'||nvl(v_day,'1');
    end date_convert;