我想单独使用select query来实现下面的代码片段o / p。是否可以不使用正则表达式?
Character Count when string input is dynamic.
DECLARE
str VARCHAR2(255);
lv_val NUMBER;
lv_char CHAR(1);
lv_unq VARCHAR2(255);
BEGIN
str:= :p_string;
FOR i IN 1..length(str)
LOOP
lv_val := 0;
lv_char := SUBSTR(str,i,1);
IF instr(lv_unq,lv_char)>0 THEN
NULL;
ELSE
lv_unq := lv_unq||lv_char;
lv_val := ((LENGTH(str) - LENGTH(REPLACE(replace(str,' ',''), lv_char, ''))) / LENGTH(lv_char));
--select ((length(str) - LENgth(REPLACE(str, lv_char, ''))) / LENgth(lv_char)) into lv_val FROM dual;
DBMS_OUTPUT.PUT_LINE('Character '||lv_char || ' is repeated :'||lv_val||' times in the string '||str);
END IF;
END LOOP;
END;
答案 0 :(得分:1)
回答问题的标题:
仅使用SQL显示字符串中重复的字符数
with v as (select substr('hello world', level, 1) c from dual connect by level < 12),
d as (select chr(ascii('a')+level-1) c from dual connect by level <= 26)
select d.c, count(v.c) from d left join v on d.c = v.c
group by d.c
order by d.c;
请参阅http://sqlfiddle.com/#!4/d41d8/38321/0了解结果
第一个视图将您的字符串拆分为字符。第二种观点只是字母表。获得两个视图后,您只需要一个带left join
子句的简单group by
来计算匹配出现次数。
请注意: