Finding and removing non ascii characters from an Oracle Varchar2

前端 未结 17 2253
猫巷女王i
猫巷女王i 2020-12-02 23:03

We are currently migrating one of our oracle databases to UTF8 and we have found a few records that are near the 4000 byte varchar limit. When we try and migrate these reco

17条回答
  •  余生分开走
    2020-12-02 23:44

    I found the answer here:

    http://www.squaredba.com/remove-non-ascii-characters-from-a-column-255.html

    CREATE OR REPLACE FUNCTION O1DW.RECTIFY_NON_ASCII(INPUT_STR IN VARCHAR2)
    RETURN VARCHAR2
    IS
    str VARCHAR2(2000);
    act number :=0;
    cnt number :=0;
    askey number :=0;
    OUTPUT_STR VARCHAR2(2000);
    begin
    str:=’^'||TO_CHAR(INPUT_STR)||’^';
    cnt:=length(str);
    for i in 1 .. cnt loop
    askey :=0;
    select ascii(substr(str,i,1)) into askey
    from dual;
    if askey < 32 or askey >=127 then
    str :=’^'||REPLACE(str, CHR(askey),”);
    end if;
    end loop;
    OUTPUT_STR := trim(ltrim(rtrim(trim(str),’^'),’^'));
    RETURN (OUTPUT_STR);
    end;
    /
    

    Then run this to update your data

    update o1dw.rate_ipselect_p_20110505
    set NCANI = RECTIFY_NON_ASCII(NCANI);
    

提交回复
热议问题