'While loop PL/SQL read a string backwards [duplicate]
i need help with this question, Using PL/SQL how can I read a string backwards using the while loop?
I know that with the for loop there is a method called reverse but I don't know how I can do it with the while loop. If you can help me I appreciate it
Solution 1:[1]
Here's one option:
SQL> set serveroutput on
SQL>
SQL> declare
2 l_str varchar2(20) := 'Littlefoot';
3 i number;
4 retval varchar2(20);
5 begin
6 i := length(l_str);
7 while i <> 0 loop
8 retval := retval || substr(l_str, i, 1);
9 i := i - 1;
10 end loop;
11 dbms_output.put_line(retval);
12 end;
13 /
toofelttiL
PL/SQL procedure successfully completed.
SQL>
If it doesn't have to be PL/SQL, hierarchical query along with listagg might be another way to do it:
SQL> var l_str varchar2(20)
SQL> exec :l_str := 'Littlefoot';
PL/SQL procedure successfully completed.
SQL> select listagg(substr(:l_str, length(:l_str) - level + 1, 1), '') within group (order by level) result
2 from dual
3 connect by level <= length(:l_str);
RESULT
--------------------------------------------------------------------------------
toofelttiL
SQL>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Littlefoot |
