대체하는 방법을 여러 함께 문자열에는 Oracle
문제
나는 문자열에서 나오는 테이블처럼"수없이 지불{1},당신의 지불{2}때문에서{3}".내가 원하는 대체{1}의 값으로,{2}의 값으로 과{3}어떤 값입니다.
그것은 가능한 대체하는 모든 3 에서 하나 교체 기능이?나입니다 찾을 수 있도록 할 수 있습니다 직접 쓰기을 쿼리하고 대체 가치입니까?내가 원하는 대체에서 이 문자열에는 Oracle 저장 프로시저는 원래의 문자열입니다에서 오는 하나의 테이블에 나는 단지 선택에는 테이블
그리고 나 대체{1},{2},{3} 에서 값을 문자열을 다른 값을 나가서 다른 테이블
해결책
비록 그것이 하나를 호출할 수 있습니다 replace()
전화:
SET mycol = replace( replace(mycol, '{1}', 'myoneval'), '{2}', mytwoval)
다른 팁
는 경우에는 많은 변수를 대체하고 당신은 그들이 또 다른 표면 변수 번호 변수를 사용할 수 있습을 재귀 CTE 을 교체합니다.예를 들어 아래.테이블 fg_rulez 당신이 문자열을 넣어의 교체입니다.테이블 fg_data 당신은 당신의 입력 문자열입니다.
set define off;
drop table fg_rulez
create table fg_rulez as
select 1 id,'<' symbol, 'less than' text from dual
union all select 2, '>', 'great than' from dual
union all select 3, '$', 'dollars' from dual
union all select 4, '&', 'and' from dual;
drop table fg_data;
create table fg_Data AS(
SELECT 'amount $ must be < 1 & > 2' str FROM dual
union all
SELECT 'John is > Peter & has many $' str FROM dual
union all
SELECT 'Eliana is < mary & do not has many $' str FROM dual
);
WITH q(str, id) as (
SELECT str, 0 id
FROM fg_Data
UNION ALL
SELECT replace(q.str,symbol,text), fg_rulez.id
FROM q
JOIN fg_rulez
ON q.id = fg_rulez.id - 1
)
SELECT str from q where id = (select max(id) from fg_rulez);
그래서,단일 replace
.
결과:
amount dollars must be less than 1 and great than 2
John is great than Peter and has many dollars
Eliana is less than mary and do not has many dollars
용어의 변수에서 오 이 중복된 질문입니다.
Oracle11gR2
숫자 값의 대체가 너무 크거나 당신이 필요할 수 있을 쉽게 유지하는것,당신은 또한 분할 문자열을 사용하여 사전 테이블이 마지막으로 집계 결과
아래 예에서 나는 가정하는 단어에서 당신의 문자열로 구분하 blankspaces 및 wordcount 에서 문자열이 되지 않을 보다 더 큰 100(pivot table 티)
with Dict as
(select '{1}' String, 'myfirstval' Repl from dual
union all
select '{2}' String, 'mysecondval' Repl from dual
union all
select '{3}' String, 'mythirdval' Repl from dual
union all
select '{Nth}' String, 'myNthval' Repl from dual
)
,MyStrings as
(select 'This is the first example {1} ' Str, 1 strnum from dual
union all
select 'In the Second example all values are shown {1} {2} {3} {Nth} ', 2 from dual
union all
select '{3} Is the value for the third', 3 from dual
union all
select '{Nth} Is the value for the Nth', 4 from dual
)
,pivot as (
Select Rownum Pnum
From dual
Connect By Rownum <= 100
)
,StrtoRow as
(
SELECT rownum rn
,ms.strnum
,REGEXP_SUBSTR (Str,'[^ ]+',1,pv.pnum) TXT
FROM MyStrings ms
,pivot pv
where REGEXP_SUBSTR (Str,'[^ ]+',1,pv.pnum) is not null
)
Select Listagg(NVL(Repl,TXT),' ') within group (order by rn)
from
(
Select sr.TXT, d.Repl, sr.strnum, sr.rn
from StrtoRow sr
,dict d
where sr.TXT = d.String(+)
order by strnum, rn
) group by strnum
를 작성하자는 동일한 샘플로 CTE 만:
with fg_rulez as (
select 1 id,'<' symbol, 'less than' text from dual
union all select 2, '>', 'greater than' from dual
union all select 3, '$', 'dollars' from dual
union all select 4, '+', 'and' from dual
), fg_Data AS (
SELECT 'amount $ must be < 1 + > 2' str FROM dual
union all
SELECT 'John is > Peter + has many $' str FROM dual
union all
SELECT 'Eliana is < mary + do not has many $' str FROM dual
), q(str, id) as (
SELECT str, 0 id
FROM fg_Data
UNION ALL
SELECT replace(q.str,symbol,text), fg_rulez.id
FROM q
JOIN fg_rulez
ON q.id = fg_rulez.id - 1
)
SELECT str from q where id = (select max(id) from fg_rulez);
만약 당신이 이렇게의 내부를 선택할 수 있습니다 그냥 작은 경우,대체 값은 열을 사용하여,문자열 연결.