Oracle中使用REGEXP_SUBSTR,regexp_replace,wm_concat函數

REGEXP_SUBSTR函數格式以下:
function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)
__srcstr     :須要進行正則處理的字符串
__pattern    :進行匹配的正則表達式
__position   :起始位置,從第幾個字符開始正則表達式匹配(默認爲1)
__occurrence :標識第幾個匹配組,默認爲1
__modifier   :模式('i'不區分大小寫進行檢索;'c'區分大小寫進行檢索。默認爲'c'。)
實際應用以下:在oracle中,使用一條語句實現將'123,456,789'拆分紅'123','456','789'的集合。
1.select REGEXP_SUBSTR('123,456,789', '[^,]+', 1, 1) value from dual;
結果是:123

2.select REGEXP_SUBSTR('123,456,789', '[^,]+', 1, 3) value from dual;css

結果是:789正則表達式

獲取一個多個數值的列,從而可以讓結果以多行的形式展現出來  oracle

1. select level from dual connect by level < 5;函數

結果是:優化

--1regexp

--2blog

--3字符串

--4it

將上面REGEXP_SUBSTR和connect by level 關聯 使用io

select TRIM(REGEXP_SUBSTR('123,456,789', '[^,]+', 1, level)) value from dual connect by level <= 3;

結果是:

--123

--456

--789

優化上面的SQL語句,讓生成的行的數量符合實際狀況 

select REGEXP_SUBSTR('123,456,789', '[^,]+', 1, level) value
from dual
connect by level <= length(regexp_replace('123,456,789', '[^,]*')) + 1;

結果是:

--123

--456

--789

 

regexp_replace函數:

1. select regexp_replace('123,456,789', '[^,]+','q') from dual;

結果:

--q,q,q

2. select regexp_replace('123,456,789', '[^,]+') from dual;

結果:

,,

 

wm_concat函數

該函數能夠把列值以","號分隔起來,並顯示成一行

效果:

select t.id_cssp_user from cssp_user_info t where rownum < 5;

select to_char(wm_concat(t.id_cssp_user))
from cssp_user_info t
where rownum < 5;

select t.mobile, to_char(wmsys.wm_concat(t.user_name))
from cssp_user_info t
where rownum<100
group by t.mobile
having count(0) > 1;

相關文章
相關標籤/搜索