Domanda

Ho una stringa proveniente da una tabella come "impossibile pagare{1}, poiché il pagamento{2} è dovuto in data {3}".Voglio sostituire {1} con un valore, {2} con un valore e {3} con un valore.

È possibile sostituirli tutti e 3 in un'unica funzione di sostituzione?o esiste un modo per scrivere direttamente la query e ottenere il valore sostituito?Voglio sostituire queste stringhe nella procedura memorizzata Oracle, la stringa originale proviene da una delle mie tabelle, sto semplicemente selezionando quella tabella

e poi voglio sostituire i valori {1},{2},{3} da quella stringa con l'altro valore che ho da un'altra tabella

È stato utile?

Soluzione

Sebbene non sia una chiamata, puoi annidare il file replace() chiama:

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

Altri suggerimenti

Se ci sono molte variabili da sostituire e le hai in un'altra tabella e se il numero di variabili è variabile puoi utilizzare un CTE ricorsivo per sostituirle.Un esempio qui sotto.Nella tabella fg_rulez metti le stringhe con la loro sostituzione.Nella tabella fg_data hai le stringhe di input.

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

Quindi, un singolo replace.

Risultato:

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

Il simbolo terminologico invece di variabile deriva da questa domanda duplicata.

Oracolo 11gR2

Se il numero di valori da sostituire è troppo grande o devi poterlo mantenere facilmente, puoi anche dividere la stringa, utilizzare una tabella di dizionario e infine aggregare i risultati

Nell'esempio seguente presumo che le parole nella stringa siano separate da spazi vuoti e che il conteggio delle parole nella stringa non sia maggiore di 100 (cardinalità della tabella pivot)

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

Scriviamo lo stesso esempio solo come 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);

Se lo stai facendo all'interno di una selezione, puoi semplicemente metterli insieme, se i valori di sostituzione sono colonne, usando la concatenazione di stringhe.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top