sql - How to reverse the string 'ab,cd,ef' to 'ef->cd->ab' -
when select table oracle, want handle 1 col'val : eg:
'ab,cd,ef' 'ef->cd->ab'; 'ab,bc' 'bc->ab'; 'acnn,bbccac' 'bbccac->acnn'; 'bbbdc,dccx,fff' 'fff->dccx->bbbdc'
we have 2 tasks. first tokenize original strings. quite easy regular expressions (although there more performant approaches if dealing large volumes). second task re-assemble tokens in reverse order; can use 11gr2 listagg() function this:
with tokens ( select distinct col1, regexp_substr(col1, '[^,]+', 1, level) tkn, level rn t23 connect level <= regexp_count (col1, '[,]') +1 ) select col1 , listagg(tkn, '->') within group (order rn desc) rev_col1 tokens group col1 /
here a sql fiddle.