如何在字符中拆分字符串并在PostgreSQL中的每个字符后添加一个新行
例如
num desc
1 Hello
2 Bye
num desc
1 H
e
l
l
o
2 B
y
e
答案 0 :(得分:2)
select num, regexp_split_to_table(descr,'')
from the_table
order by num;
SQLFiddle:http://sqlfiddle.com/#!15/13c00/4
然而,角色的顺序并不能保证,并且实现这一点有点复杂。
以Erwin's answer regarding this problem:
为基础select case
when row_number() over (partition by id order by rn) = 1 then id
else null
end as id_display,
ch_arr[rn]
from (
select *,
generate_subscripts(ch_arr, 1) AS rn
from (
select id,
regexp_split_to_array(descr,'') as ch_arr
from data
) t1
) t2
order by id, rn;
修改强>
如果您只想为每个id分配一个字符串,其中字符由换行符分隔,您可以使用:
select id,
array_to_string(regexp_split_to_array(descr,''), chr(10))
from data
order by id