1

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.')

2 Answers 2

2
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
Sign up to request clarification or add additional context in comments.

Comments

1

You can use string_agg and FORMAT:

SELECT string_agg(FORMAT('x.%s = z.%s', t,t) , ',')
FROM (SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) AS t
     ) AS sub;

You can use as delimeter E',\r\n' to get carriage return:

SELECT string_agg(FORMAT('x.%s = z.%s', t,t) , E',\r\n')
FROM (SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) AS t)
      AS sub

Output:

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.G = z.G,
x.H = z.H,
x.I = z.I,
x.J = z.J,
x.K = z.K,
x.L = z.L 

2 Comments

@MAK I thought you want one result set not multiple. Please be more specific next time.
Okay! I apologize for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.