Siva Academy

Youtube.com/SivaAcademy  Oracle SQL  |  PLSQL  |  SQL PLSQL Interview Questions  |  PLSQL Scenario based Interview Questions  |  Subscriber Questions & Answers

  |  SQL practical Question  |  SQL Performance Tuning  |  New Features  |  

  Trigger     |    View  

Saturday, June 22, 2019

Sort the NUMBERS in the comma separated list




select regexp_substr('A,BB,CCC,DDDD','[^,]+',1,1) from dual;
select regexp_substr('A,BB,CCC,DDDD','[^,]+',1,2) from dual;
select regexp_substr('A,BB,CCC,DDDD','[^,]+',1,3) from dual;

select regexp_substr('A,BB,CCC,DDDD','[^,]+',1,4) from dual;

-------------------------------------------------------------

WITH t AS (
    SELECT  '3,10,5,1,7,55,75,100,42,12,17' d
    FROM         dual

  SELECT  LISTAGG(s,',') WITHIN GROUP(
    ORDER BY to_number(s))
  FROM(
        SELECT regexp_substr(d,'[^,]+',1,level) s
        FROM   t
        CONNECT BY level <= regexp_count(d,',') + 1
    );

-------------------------------------------------------------

WITH t AS (
    SELECT '1 5 3 6 2 4   7  99   10   76    101    55   15  33 18' d
    FROM dual
) SELECT LISTAGG(s,' ') WITHIN GROUP(ORDER BY to_number(rtrim(ltrim(s) ) ))
  FROM(
        SELECT regexp_substr(regexp_replace(d,'\D+',','),'[^,]+',1,level) s
        FROM t
        CONNECT BY level <= regexp_count(regexp_replace(d,'\D+',','),',') + 1
    );

    
-------------------------------------------------------------

WITH t AS (
    SELECT  'WELCOME' d
    FROM dual
) SELECT LISTAGG(d) WITHIN GROUP( ORDER BY d )
  FROM(
        SELECT substr(d,level,1) d
        FROM t

        CONNECT BY level <= length(d));

No comments:

Post a Comment