Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Converter uma série de valores numéricos em texto no Oracle SQL Query


Aqui está uma opção. Leia comentários dentro do código. Dados de amostra nas linhas #1 - 13; consulta começa na linha #14.
SQL> with
  2  expl (id, name) as
  3    (select 1, 'car' from dual union all
  4     select 2, 'bus' from dual union all
  5     select 3, 'BB'  from dual union all
  6     select 4, 'SB'  from dual union all
  7     select 5, 'Ba'  from dual union all
  8     select 6, 'PA'  from dual union all
  9     select 7, 'HB'  from dual union all
 10     select 8, 'G'   from dual
 11    ),
 12  temp (col) as
 13    (select '1,4,7,8' from dual),
 14  -- split COL to rows
 15  spl as
 16    (select regexp_substr(col, '[^,]+', 1, level) val,
 17            level lvl
 18     from temp
 19     connect by level <= regexp_count(col, ',') + 1
 20    )
 21  -- join SPL with EXPL; aggregate the result
 22  select listagg(e.name, ',') within group (order by s.lvl) result
 23  from expl e join spl s on s.val = e.id;

RESULT
--------------------------------------------------------------------------------
car,SB,HB,G

SQL>