Convert the given numbers into words 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.
Given a number and task is to convert each digit of the number into words.
Examples:
Input: 47856 Output: Four Seven Eight Five Six Input: 123456 Output: one two three four five six
Approach is to select the corresponding word of a particular number using decode function.
Below is the required implementation:
DECLARE -- declare variable are num, num_to_word, str, len, c -- and in above declare variable num, len, c are integer datatype -- and num_to_word and str are varchar datatype num INTEGER; num_to_word VARCHAR2(100); str VARCHAR2(100); len INTEGER; c INTEGER; BEGIN num := 123456; len := Length(num); dbms_output.Put_line('Entered Number is: ' ||num); FOR i IN 1..len LOOP c := Substr(num, i, 1); SELECT Decode(c, 0, 'Zero ', 1, 'One ', 2, 'Two ', 3, 'Three ', 4, 'Four ', 5, 'Five ', 6, 'Six ', 7, 'Seven ', 8, 'Eight ', 9, 'Nine ') INTO str FROM dual; num_to_word := num_to_word ||str; END LOOP; dbms_output.Put_line('Number to words: ' ||num_to_word); END; -- Program End |
Output :
Entered Number is: 123456 Number to words: One Two Three Four Five Six
Recommended Posts:
- Difference between SQL and PLSQL
- Count no. of characters and words in a string in PL/SQL
- Convert distance from km to meters and centimeters in PL/SQL
- Sum Of Two Numbers in PL/SQL
- GCD of two numbers in PL/SQL
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Sum and average of three numbers in PL/SQL
- Swap two numbers in PL/SQL
- Print all even numbers from 1 to n in PL/SQL
- Swap two numbers in PL/SQL without using temp
- Greatest number among three given numbers in PL/SQL
- Finding sum of first n natural numbers in PL/SQL
- Display Sequence of Numbers in SQL Using LEVEL
- SELECT INTO statement in SQL
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.



