Andrew Ramnikov
Andrew Ramnikov

Reputation: 793

How to split String and insert it as new line in the same table?

i have a table Address.

create table address(
id  number,
city text,
street text,
house_number text);

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

With regard

Andrey

Upvotes: 1

Views: 1564

Answers (1)

Elad
Elad

Reputation: 894

Use select unnest(string_to_array(YOUR STRING, '-'));

it will convert '3-5-7' to:

3
5
7

Then you can simply run a query that will insert the rows to your table. See SQLFiddle.

Upvotes: 2

Related Questions