【问题标题】:how to separate comma separated values in oracle 11G如何在 oracle 11G 中分隔逗号分隔值
【发布时间】:2014-12-19 17:31:43
【问题描述】:

我需要用逗号分隔列的值。

示例:BCDEY;我需要转换为B, C, D, E, Y。 跟随“选择”:

SELECT CDGRUPOCONDICAO FROM TBINTCLIENTE;

【问题讨论】:

    标签: oracle oracle11g


    【解决方案1】:

    你也可以试试这个:

    with cad as  
    (select 'BCDEY' cad from dual)
    select regexp_replace (regexp_replace(cad,'(.)','\1, '),', $','') cad_comma from cad;
    

    【讨论】:

      【解决方案2】:

      可能是这样的吗?

      with testdata as (select 'BCDEY' str from dual)
      
      select listagg(c, ', ') within group(order by lvl)
      from (
        select substr(str, level, 1) c,
               level lvl
        from testdata
        connect by level <= length(str)
        )
      

      制作:

      B, C, D, E, Y
      

      这里,子查询逐个字符地分割字符串。然后外部listagg 重新组装项目,将它们与', ' 连接起来。

      【讨论】:

        【解决方案3】:

        另一种解决方案,使用recursive subquery factoring(因此,假设 oracle >= 11g 第 2 版):

        with testdata as (select 1 id, 'BCDEY' str from dual union all
                          select 2, 'ABC' from dual),
        --                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        --                 replace that subquery by your actual query
             splited(c,r,id,lvl) as (
                          select '' c, str r, id, 0 lvl from testdata
                          union all
                          select substr(r, 1, 1) c,
                                 substr(r, 2) r,
                                 id,
                                 lvl+1
                          from splited
                          where r is not null)
        
        select listagg(c, ', ') within group(order by lvl)
        from splited
        group by (id)
        

        制作:

        LISTAGG(C,',')WITHINGROUP(ORDERBYLVL)
        B, C, D, E, Y
        A, B, C
        

        http://sqlfiddle.com/#!4/d41d8/38971

        【讨论】:

          猜你喜欢
          • 2017-07-04
          • 2015-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多