Reverse a number in PL/SQL
Prerequisite – PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements.
In declare part, we declare variables and between begin and end part, we perform the operations.
Here, first, we take three variables num, len, and revnum and assign the value in num and fin is the length of the number and after finding the length we start a loop from maximum length to starting length, we assign the num into revnum and print revnum.
Examples:
Input : 123456 Output :654321
Input :87459 Output :95478
Below is the required implementation:
declare -- declare variable num , len -- and revnum of datatype varchar num varchar2(5):='12345'; len number(2); revnum varchar2(5); begin -- Here we find the length of string len := length(num); -- here we starting a loop from max len to 1 for i in reverse 1.. len loop -- assigning the reverse number in revnum revnum := revnum || substr(num,i,1); end loop; -- Print the Result dbms_output.put_line('given number ='|| num); dbms_output.put_line('reverse number ='|| revnum); end; -- Program End |
Output :
reverse of number is 654321.
Recommended Posts:
- Difference between SQL and PLSQL
- PLSQL | CHR Function
- PLSQL | ABS Function
- PLSQL : || Operator
- PLSQL | TRIM Function
- PLSQL | LENGTH2 Function
- PLSQL | INSTR4 Function
- PLSQL | REPLACE Function
- PLSQL | LPAD Function
- PLSQL | NCHR Function
- PLSQL | INSTRC Function
- PLSQL | LOWER Function
- PLSQL | LTRIM Function
- PLSQL | RTRIM Function
- PLSQL | ATAN Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



