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?
CodePudding user response:
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}'