Oracle字串截取和拼接應用

來源:互聯網
上載者:User

標籤:字串截取 拼接 substr instr

今天有人問了個關於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

本文出自 “QYtag (Upspringing)” 部落格,請務必保留此出處http://qytag.blog.51cto.com/6125308/1627680

Oracle字串截取和拼接應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.