Sha*_*ath 7 regex sql string oracle
如何计算Oracle中某列中特定字符出现的次数?例如,如果我有一个FOO包含a,ABC,def和的数据的表2,3,4,5,我想计算逗号在数据中出现的次数.
CREATE TABLE foo (
str varchar2(30)
);
INSERT INTO foo VALUES( 'a,ABC,def' );
INSERT INTO foo VALUES( '2,3,4,5' );
commit;
Run Code Online (Sandbox Code Playgroud)
我想要的输出是
str cnt
a,ABC,def 2
2,3,4,5 3
Run Code Online (Sandbox Code Playgroud)
Mat*_*Mat 30
这方面一个惯用的伎俩是使用组合length和replace:
select (length(your_col) - length(replace(your_col, ','))) from your_table;
Run Code Online (Sandbox Code Playgroud)
replace 没有第三个参数将简单地删除该字符.
Jus*_*ave 22
根据您的Oracle版本(regexp_count在11.1中引入),我倾向于发现做类似的事情更清楚
SELECT regexp_count( <<column_name>>, ',' )
FROM <<table_name>>
Run Code Online (Sandbox Code Playgroud)
您可以在桌子上看到它
SQL> ed
Wrote file afiedt.buf
1 select str, regexp_count( str, ',' )
2* from foo
SQL> /
STR REGEXP_COUNT(STR,',')
------------------------------ ---------------------
a,ABC,def 2
2,3,4,5 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76088 次 |
| 最近记录: |