Split comma separated values to columns in Oracle

后端 未结 4 1689
温柔的废话
温柔的废话 2020-11-22 01:58

I have values being returned with 255 comma separated values. Is there an easy way to split those into columns without having 255 substr?

ROW  | VAL
--------         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 02:31

    hierarchical query could be used. pivoting can be done with case and group by.

    with value_t as
    ( 
      select row_t,row_number() OVER (partition by row_t order by rownum )rn,
      regexp_substr(val, '[^,]+', 1, LEVEL) val from Table1
    CONNECT BY LEVEL <= regexp_count(val, '[^,]+') 
    AND prior row_t = row_t 
    AND prior sys_guid() is not null
      ) select row_t, max( case when rn = 1 THEN val end ) val_1,
      max( case when rn = 2 THEN val end ) val_2,
      max( case when rn = 3 THEN val end ) val_3
      from value_t
      group by row_t;
    

提交回复
热议问题