简体   繁体   中英

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?

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

select t.employee_name,
       x.role[1] as role,
       x.pos,
       t.date_joined
from the_table t
  cross join regexp_matches(t.role, '[0-9]{4}', 'g') with ordinality as x(role, pos)       
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM