Search code examples
postgresqlpostgresql-9.3string-function

PostgreSQL 9.3: REPLACE string


I have a string to replace with the expected form.

Input: I have the following string.

'A,B,C,D,E,F,G,H,I,J,K,L'

And I want to replace the above string into the following format:

'x.A = z.A ,
 x.B = z.B , 
 x.C = z.C , 
 x.D = z.D , 
 x.E = z.E , 
 x.F = z.F ,
 .........
 .........
 x.L = z.L'

My try:

SELECT 'x.'||REPLACE('A,B,C,D,E,F,G,H,I,J,K,L',',',' = z.')

Solution

  • SELECT 'x.' || col || '=z.' || col
    FROM (
          SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) col
         ) t