2008-09-17 4 views
9

"{1}을 (를) 지불 할 수 없습니다 (2) {3}에 지불해야합니다."와 같은 테이블에서 문자열이 왔습니다. {1}을 (를) 어떤 값으로, {2}를 (를) 약간의 값으로 {3}을 일부 값으로 대체하려고합니다.오라클에서 여러 문자열을 함께 바꾸는 방법

하나의 바꾸기 기능에서 3을 모두 바꿀 수 있습니까? 또는 직접 쿼리를 작성하고 값을 대체 할 수있는 방법이 있습니까? 나는 오라클 저장 프로 시저에서 이러한 문자열을 교체하고 싶습니다. 원래의 문자열이 내 테이블에서오고 있습니다. 그냥 그 테이블에서 선택하고 있습니다.

그리고 나서 {1}, {2}, {3} 값을 대체하고 싶습니다. 당신이 할 수있는,

SET mycol = replace(replace(mycol, '{1}', 'myoneval'), '{2}', mytwoval) 
+0

가능한 복제 [보다 큰되지 않습니다 있으리라 믿고있어 아래 function in Oracle] (https://stackoverflow.com/questions/2947623/multiple-replace-function-in-oracle) – Joaquinglezsantos

답변

12

에서이 다른 값으로 해당 문자열에서 그것을 할 수 있습니다 중첩 replace() 전화 한 통화 아니지만 문자열 값 연결을 사용하여 대체 값이 열인 경우이를 함께 조각화하십시오.

+0

두 번째 mytwoval에 작은 따옴표가 누락되어 있습니까? –

+0

@SimonTheCat 맞습니다! (mytwoval이 변수가 아닌 한) – hamishmcn

-1

당신은 선택의 내부를 수행하는 경우 : 내가 다른 테이블

4

대체 할 변수가 많고 다른 테이블에 변수가 있고 가변 개수가 가변적 인 경우 재귀 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 

대신 변수의 용어 기호 대체 할 수있는 값의 개수가 너무 큰 경우 this duplicated question.에서

오라클 11gR2

+0

WITH 절의 별칭은 Oracle 11gR2부터 지원됩니다. – Stephan

+1

IMH이 질문은 슬프게도 질문에서 오직 기울어 진 부분만을 언급하기 때문에 즉, 실제 쿼리를 수정하지 않고 대체 된 조건을 업데이트 할 수있게 해주는 훌륭한 답변입니다 (제어 된 환경에서 유용한 시나리오 임). – Neil

1

를 제공하거나 할 수 있어야합니다 쉽게 유지 관리하기 위해 문자열을 분할하고 사전 테이블을 사용하여 결과를 최종적으로 집계 할 수 있습니다.

예에서 6,

나는 당신의 문자열에서 단어 blankspaces로 구분하고 여러 REPLACE 문자열의 단어 수는 100 (피벗 테이블 카디)의

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 
+0

SQL-only 솔루션을 구현하기 위해 WITH 절과 명성을 사용하는 것이 좋습니다. 그러나 결과는 구문 분석하기가 여전히 어렵습니다! 멋지게 작동하는 것처럼 보이지만 코드로 구현 한 경우 동료에게 정확성을 검토하도록 요청하지는 않습니다. 그들은 나를 쏠거야! :-) – StewS2

관련 문제