Replace multiple characters within a string in PostgreSQL

0

I have the following problem.

I use PostgreSQL and I have a string with multiple values that are repeated. I need to take that value and replace it with another value from another table.

Example

  

String cadena = "Se actualizarón ordenes de ejecución, de valores: Tipo de mensaje_1,Posición_3Tipo de mensaje_2,Posición_2Tipo de mensaje_3,Posición_1";

What I need is to replace all the characters that are after the hyphen under "_" with a value from another table.

ID | VALUE
1 | One
2 | Two
3 | Three

To make the chain look like this:

  

String cadena = "Se actualizarón ordenes de ejecución, de valores: Tipo de mensaje_Uno,Posición_3Tipo de mensaje_Dos,Posición_2Tipo de mensaje_Tres,Posición_1";

That's what I do in a PostgreSQL function, so I need to use PL / PGSQL. It should be noted that the characters I need to replace may appear N times inside the String.

Any help?

    
asked by Rick Nuñez 15.03.2017 в 00:13
source

1 answer

0

According to the official documentation of PostgreSQL ( link ), it is made the following way:

Example:

replace('abcdefabcdef', 'cd', 'XX')

Result:

abXXefabXXef

So, let's go in parts:

We replace all 1:

replace('Se actualizarón ordenes de ejecución, de valores: Tipo de mensaje_1,Posición_3Tipo de mensaje_2,Posición_2Tipo de mensaje_3,Posición_1','1','Uno')

All 2:

replace('Se actualizarón ordenes de ejecución, de valores: Tipo de mensaje_1,Posición_3Tipo de mensaje_2,Posición_2Tipo de mensaje_3,Posición_1','2','Dos')

All 3:

replace('Se actualizarón ordenes de ejecución, de valores: Tipo de mensaje_1,Posición_3Tipo de mensaje_2,Posición_2Tipo de mensaje_3,Posición_1','3','Tres')
    
answered by 15.03.2017 в 00:32