5

I have have table named BookInfo with the following structure:

|id  |book_name  |description  |
--------------------------------
|1   |book 1     |harry        |
|    |           |potter       |
|    |           |Part 2       |
|2   |...        |             |

How could I split the row (id=1) into multiple rows on the newline character (so that harry \n potter \n Part 2 would be separated into 3 different records: harry, potter and Part 2 using a query?

To reiterate, the resultset would look something like this:

|id  | description   |
----------------------
|1   |harry          | 
|2   |potter         |
|3   |Part 2         |

Any help will be much appreciated, thank you.

1 Answer 1

15

You're looking for regexp_split_to_table():

http://www.postgresql.org/docs/9.2/static/functions-string.html

select regexp_split_to_table('hello world', E'\\s+');

hello
world

(2 rows)
Sign up to request clarification or add additional context in comments.

Comments

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.