我对 PL/SQL 很陌生,我需要获取字符串中不同字符的名称和计数。例如,如果我有一个字符串str="helloexample"
,我需要得到不同字符的输出str
,即heloxamp
。
我怎样才能做到这一点?
我对 PL/SQL 很陌生,我需要获取字符串中不同字符的名称和计数。例如,如果我有一个字符串str="helloexample"
,我需要得到不同字符的输出str
,即heloxamp
。
我怎样才能做到这一点?
您可以按如下方式使用正则表达式:
SET serveroutput ON
DECLARE
str VARCHAR2(20):='helloexample';
str_length NUMBER;
c VARCHAR2(20):=NULL;
d NUMBER;
BEGIN
str_length:=LENGTH(str);
FOR i IN 1..str_length
LOOP
IF regexp_instr(c,SUBSTR(str,i,1))>0 THEN
NULL;
ELSE
c:=c||SUBSTR(str,i,1);
END IF;
END LOOP;
dbms_output.put_line(c);
END;
答案是:
heloxamp
如果 WM_CONCAT 不适合你,你可以使用这个技巧:
select rtrim(xmlagg(xmlelement(e, str)).extract('//text()'), ',') as output
from (select substr(str, level, 1) str, level l,
row_number() over (partition by substr(str, level, 1)
order by level) rn
from (select 'helloexample' str from dual) d
connect by level <= length(str)
order by level)
where rn = 1;
SQL> select rtrim(xmlagg(xmlelement(e, str)).extract('//text()'), ',') as output
2 from (select substr(str, rownum, 1) str, level l,
3 row_number() over (partition by substr(str, rownum, 1) order by level) rn
4 from (select 'helloexample' str from dual) d
5 connect by level <= length(str)
6 order by level)
7 where rn = 1;
OUTPUT
--------------------------------------------------------------------------------
heloxamp