Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split rows into multiple rows based on string length of column Postgresql

I have the following table

+---------------+---------------+-------------+
| employee_name |     role      | date_joined |
+---------------+---------------+-------------+
| John          |      10013004 | 2018-01-09  |
| Jane          |          1004 | 2020-08-09  |
| Sam           |  100380003000 | 2022-03-31  |
+---------------+---------------+-------------+

I want to convert the above table in the format below, where the role column string should be split into groups of 4 and should be added as new entry.

+---------------+-------+-------------+
| employee_name | role  | date_joined |
+---------------+-------+-------------+
| John          |  1001 | 2018-01-09  |
| John          |  3004 | 2018-01-09  |
| Jane          |  1004 | 2020-08-09  |
| Sam           |  1003 | 2022-03-31  |
| Sam           |  8000 | 2022-03-31  |
| Sam           |  3000 | 2022-03-31  |
+---------------+-------+-------------+

Any idea how can I achieve the following?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use regexp_matches() to generate rows with those 4 character substrings:

select t.employee_name,
       x.role[1] as role,
       t.date_joined
from the_table t
  cross join regexp_matches(t.role, '[0-9]{4}', 'g') as x(role)       
order by t.employee_name, t.date_joined

regexp_matches() returns an array of matches, that’s why the x.role[1] is required.

If the column can contain other characters, not just numbers, use '.{4}' instead of '[0-9]{4}'

Online example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading