Reverse String Word by Word using SQL

后端 未结 5 1468
半阙折子戏
半阙折子戏 2020-12-15 13:19

I would need to reverse the word positions in a sentence or String.

For example : \"Hello World! I Love StackOverflow\", to be displayed as \"StackOverflow          


        
5条回答
  •  轮回少年
    2020-12-15 14:10

    DECLARE 
    
    in_string VARCHAR2(500);
    pros_string  VARCHAR2(500);
    out_string  VARCHAR2(800);  
    spce_cnt NUMBER;
    
    
    BEGIN
    
    in_string := 'Hello World! I Love StackOverflow';
    pros_string := ' '||in_string||' ' ;   
    spce_cnt := REGEXP_COUNT(pros_string,' ',1);
    FOR i IN reverse  1.. spce_cnt-1  
    LOOP                               
     out_string := out_string||' '|| SubStr (pros_string,InStr(pros_string, ' ',1,i)+1 ,InStr(SubStr (pros_string,InStr(pros_string, ' ',1,i)+1 ),' '  ));    
    Dbms_Output.Put_Line(out_string);    
    END LOOP; 
    END;
    

提交回复
热议问题