Oracle10g新特性——Regex - 轉

來源:互聯網
上載者:User

標籤:

在進行查詢時,有時候需要按照一定的特殊規則來尋找某個字串,比如,你可能需要查詢第三位為5-8,最後四位為’8888’的所有電話。在9i之前,你可能需要寫一個很複雜的條件:
Select username from t_userinfowhere (phonenumber like ‘135%8888’or phonenumber like ‘136%8888’or phonenumber like ‘137%8888’or phonenumber like ‘138%8888’)and length(phonenumber) = 13;

    那時就會很羨慕java程式員可以使用一個Regex輕鬆搞定。10g中,再也不需要這麼複雜了, oracle也提供了幾個Regex函數,大大方便了開發人員:REGEXP_LIKE、REGEXP_REPLACE、REGEXP_INSTR、REGEXP_SUBSTR,分別用於模糊比對、代替、插入、截取字串。關於Regex的規則這就不詳細描述了, 可以查相關資料得到。簡單舉例。以上面例子為例,我們的查詢語句可以寫成: 

SQL> create table t_userinfo (username varchar2(10), phonenumber varchar2(13));Table createdSQL> insert into t_userinfo values (‘zhansan‘, ‘13012323434‘);1 row insertedSQL> insert into t_userinfo values (‘lisi‘, ‘13512348888‘);1 row insertedSQL> insert into t_userinfo values (‘wangwu‘, ‘13912328888‘);1 row insertedSQL> insert into t_userinfo values (‘zhaoliu‘, ‘13743218888‘);1 row insertedSQL> insert into t_userinfo values (‘sunqi‘, ‘1361234888‘);1 row insertedSQL> commit;Commit completeSQL> Select username, phonenumber from t_userinfo2 where REGEXP_LIKE(phonenumber, ‘13[5-8][0-9][0-9][0-9][0-9]8{4}‘);USERNAME PHONENUMBER---------- ------------- lisi 13512348888zhaoliu 13743218888

 

Oracle10g新特性——Regex - 轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.