使用PLSQL對字串進行排序,plsql字串排序

來源:互聯網
上載者:User

使用PLSQL對字串進行排序,plsql字串排序
1.建Function程式

CREATE OR REPLACE FUNCTION sort_string(p_string IN VARCHAR2) RETURN VARCHAR2 IS  TYPE typ_rec IS RECORD(    letter VARCHAR2(256), --記錄字母    seq    PLS_INTEGER --記錄字母出現次數    );  TYPE tab_rec IS TABLE OF typ_rec INDEX BY BINARY_INTEGER;  tabr     tab_rec;  l_result VARCHAR2(32767);BEGIN  FOR i IN 1 .. length(p_string) LOOP    tabr(ascii(substr(p_string, i, 1))).letter := substr(p_string,                                                         i,                                                         1);    tabr(ascii(substr(p_string, i, 1))).seq := nvl(tabr(ascii(substr(p_string, i, 1))).seq,                                                   0) + 1;  END LOOP;  FOR i IN tabr.first .. tabr.last LOOP    IF tabr.exists(i) THEN      l_result := l_result || rpad(tabr(i).letter,                                   tabr(i).seq,                                   tabr(i).letter);    END IF;  END LOOP;  RETURN l_result;END;

2.用法 例子1:
SQL> SELECT sort_string ('43258888') FROM DUAL; SORT_STRING('43258888')--------------------------------------------------------------------------------23458888
例子2:
SQL> SELECT sort_string ('klaktkky') FROM DUAL; SORT_STRING('KLAKTKKY')--------------------------------------------------------------------------------akkkklty

文本地址:http://blog.csdn.net/sunansheng/article/details/46331485

相關文章

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.