comma Separated List

前端 未结 2 945
忘了有多久
忘了有多久 2020-12-22 10:59

I have procedure that has parameter that takes comma separated value , so when I enter Parameter = \'1,0,1\'

I want to return \' one , Zero , One\' ?

相关标签:
2条回答
  • 2020-12-22 11:17

    This query splits list into into numbers, converts numbers into words and joins them again together with function listagg:

    with t1 as (select '7, 0, 11, 132' col from dual),
         t2 as (select level lvl,to_number(regexp_substr(col,'[^,]+', 1, level)) col 
                  from t1 connect by regexp_substr(col, '[^,]+', 1, level) is not null)
    select listagg(case 
                     when col=0 then 'zero' 
                     else to_char(to_date(col,'j'), 'jsp') 
                   end, 
                   ', ') within group (order by lvl) col
      from t2
    

    Output:

    COL
    -------------------------------------------
    seven, zero, eleven, one hundred thirty-two
    

    The limitation of this solution is that values range is between 0 and 5373484 (because 5373484 is maximum value for function to_date). If you need higher values you can find hints in this article.

    0 讨论(0)
  • 2020-12-22 11:21

    You could use REPLACE function.

    For example,

    SQL> WITH DATA(str) AS(
      2  SELECT '1,0,1' FROM dual
      3  )
      4  SELECT str,
      5         REPLACE(REPLACE(str, '0', 'Zero'), '1', 'One') new_str
      6  FROM DATA;
    
    STR   NEW_STR
    ----- ------------------------------------------------------------
    1,0,1 One,Zero,One
    
    SQL>
    
    0 讨论(0)
提交回复
热议问题