I'm trying to adapt a recursive cte with regexp_replace. Instead of using a comma as a separator I'm using a pipe-character. I can't get to work.
drop table if exists lc_colors;
create table lc_colors (id int, colors text);
insert into lc_colors
values ( 1, 'Red|Green|Blue')
, ( 2, 'Orange|Cyan')
, ( 3, 'Black|White|Grey')
, ( 4, 'Yellow|Blue');
with recursive unwound as (select *
from lc_colors lc
union all
select id
, regexp_replace(colors, '^[^|]*|', '') colors
from unwound
where colors like '%|%')
select id
, regexp_replace(colors, '|.*', '') colors
from unwound
order by id;
the original question I'm trying to adapt
solution ==>>: Since I don't have enough credits with this account I edit my question with my (adapted) solution. It's in escaping the pipe-character (\|):
with recursive unwound as (select *
from speeltuin.lc_colors_pipe lc
union all
select id
, regexp_replace(colors, '^[^\\|]*\\|', '') colors
from unwound
where colors like '%|%')
select id
, regexp_replace(colors, '\\|.*', '') colors
from unwound
order by id;