1

我有一个字符串要替换为预期的形式。

输入:我有以下字符串。

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

我想将上面的字符串替换为以下格式:

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

我的尝试

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

2 回答 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
于 2016-01-06T10:04:38.070 回答
1

您可以使用string_aggFORMAT

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;

您可以使用分隔符E',\r\n'来获得回车:

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

输出:

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 
于 2016-01-06T10:06:15.727 回答