programing

Oracle에서 여러 문자열을 함께 바꾸는 방법

iphone6s 2023. 9. 9. 09:08
반응형

Oracle에서 여러 문자열을 함께 바꾸는 방법

{3}에 지불해야 하는 {2}이(가) 기한이므로 {1}을(를) 지불할 수 없습니다."와 같은 테이블에서 오는 문자열이 있습니다.{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

변수 대신 용어 기호가 이 중복된 질문에서 유래합니다.

오라클 11gR2

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);

교체할 값의 수가 너무 많거나 쉽게 유지할 수 있어야 할 경우 문자열을 분할하여 사전 테이블을 사용한 후 결과를 집계할 수도 있습니다.

아래 예제에서 문자열의 단어는 빈칸으로 구분되어 있고 문자열의 단어 수는 100보다 크지 않다고 가정합니다(pivot table cardinality).

    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 is used to split the stings from MyStrings. We use a cartesian join for this
    ,pivot as (
      Select Rownum Pnum
      From dual
      Connect By Rownum <= 100   
      )
    -- StrtoRow is basically a cartesian join between MyStings and Pivot. 
-- There as many rows as individual string elements in the Mystring Table
-- (Max = Numnber of rows Mystring table * 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
    )
    -- This is the main Select. 
    -- With the listagg function we group the string together in lines using the key strnum (group by)
   -- The NVL gets the translations: 
       -- if there is a Repl (Replacement from the dict table) then provide it, 
       -- Otherwise TXT (string without translation)
    Select Listagg(NVL(Repl,TXT),' ') within group (order by rn) 
    from
    (
    -- outher join between strings and the translations (not all strings have translations)
    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

선택 항목 내부에서 이 작업을 수행하는 경우 대체 값이 열인 경우 문자열 연결을 사용하여 항목을 결합할 수 있습니다.

언급URL : https://stackoverflow.com/questions/83856/how-to-replace-multiple-strings-together-in-oracle

반응형