如何拆分String并将其作为新行插入到同一表中?
问题描述:
我有一个表地址。
create table address(
id number,
city text,
street text,
house_number text);
有时候我得到城市=柏林,街道= xyz和房屋编号
而不是3,例如3-5或3-5-7。
some times i get city = Berlin, street = xyz and house_number instead 3 ,for example, 3-5 or 3-5-7.
在这种情况下,我希望拆分此数字并创建新的行,以便得到:
i want in this case to split this numbers and crate new line/s so i get :
id city street house_number
1. Berlin xyz 3
2. Berlin xyz 5
2. Berlin xyz 7
关于
安德烈
答
使用选择unnest(string_to_array(YOUR STRING,'-'));
它将将'3-5-7'
转换为:
it will convert '3-5-7'
to:
3
5
7
然后,您可以简单地运行将表中的行插入查询。
请参见 SQLFiddle 。
Then you can simply run a query that will insert the rows to your table. See SQLFiddle.