Oracle字串截取和拼接應用

來源:互聯網
上載者:User

Oracle字串截取和拼接應用

今天有人問了個關於Oracle字串截取和拼接的問題,讓我幫他寫出SQL,看了下問題描述還比較清晰就試著解決下,利用午休時間把功能實現了,問題看似不難,但思路一定要清晰,不然就亂了,關鍵大量應用了Oracle的substr 和instr函數。下面貼出問題和指令碼:
問題:sql中一個欄位值為:1788987565327、768374872394903、21437238740213483874629、23412341234252345。其中頓號間隔的每一組數字位元和尾數不定,現在要使前面這個欄位值中頓號前的數字尾數即7、3、9、5都分別加1,變成8、4、0、6輸出成1788987565328、768374872394904、21437238740213483874620、23412341234252346。注意其中第三個數,從9加1後,輸出成0,而不是10

指令碼:
declare
    targetstr varchar2(2000);
    strlength number;
    position number;
    maxposition number;
    retrunstr varchar2(2000);
    tempstr varchar2(2000);
    endstr number;
begin
    targetstr := '1788987565327、768374872394903、21437238740213483874629、23412341234252345';
    maxposition := 0;
    select LENGTH(targetstr) into strlength  from dual;
    for i in 1..strlength loop
        select instr(str,'、',1,i) into position from (select targetstr as str from dual);
        --dbms_output.PUT_LINE(position);
        if position > 0 then
            if maxposition = 0 then
                select substr(str,0,instr(str,'、',1,1)-1) into retrunstr from (select targetstr as str from dual);
                select TO_NUMBER(substr(restr,-1)) into endstr from (select retrunstr as restr from dual);
                if endstr = 3 or endstr = 5 or endstr = 7 then
                    endstr := endstr + 1;
                elsif endstr = 9 then
                    endstr := 0;
                end if;
                select substr(str,0,instr(str,'、',1,1)-2)||TO_CHAR(endstr) into retrunstr from (select targetstr as str from dual);
            elsif maxposition < position then
                select substr(str,instr(str,'、',1,i-1)+1,instr(str,'、',1,i)-instr(str,'、',1,i-1)-1) into tempstr from (select targetstr as str from dual);
                select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
                if endstr = 3 or endstr = 5 or endstr = 7 then
                    endstr := endstr + 1;
                elsif endstr = 9 then
                    endstr := 0;
                end if;
                select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
                retrunstr := retrunstr || '、'|| tempstr;             
            end if;
            maxposition := position;
        else
            if maxposition > position then
                --特別處理最後一段
                tempstr := '';
                select substr(str,maxposition-length(str)) into tempstr from (select targetstr as str from dual);
                select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
                if endstr = 3 or endstr = 5 or endstr = 7 then
                    endstr := endstr + 1;
                elsif endstr = 9 then
                    endstr := 0;
                end if;
                select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
                retrunstr := retrunstr || '、'|| tempstr;
            end if;
            exit;
        end if;
    end loop;
    dbms_output.PUT_LINE(retrunstr);
end;

運行結果如下:
原字串:1788987565327、768374872394903、21437238740213483874629、23412341234252345
14:03:38  **** SCRIPT STARTED: 02-Apr-2015 14:03:38 ****
14:03:38  declare
14:03:38      targetstr varchar2(2000);
14:03:38  ...
14:03:39  PL/SQL block executed
1788987565328、768374872394904、21437238740213483874620、23412341234252346
14:03:39  **** SCRIPT ENDED 02-Apr-2015 14:03:39 ****
14:03:39  End Script Execution

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.