Wednesday 11 September 2019

Postgresql split column into rows | Postgres delimited split rows

Problem:

Table structure:


Roll No Name Hobbies
1 Ram Dance,Playing
2 Alex Music,Movies,Internet

Now we want to split hobbies into rows based on comma(,).

to get our results like this:

1  Ram  Dance
1  Ram  Playing
2  Alex  Music
2  Alex  Movies
2  Alex  Internet


To get this output you can use below query in postgres:

SELECT 
    Roll No, Name
    regexp_split_to_table(Hobbies, E',')  as Hobbie
FROM Tablename

Use your delimiter and replace table name with your table name.

No comments:

Post a Comment