Search
 
SCRIPT & CODE EXAMPLE
 

SQL

amount to words oracle Function

CREATE OR REPLACE FUNCTION amount_in_words (i_amt IN NUMBER)
   RETURN VARCHAR2
IS
   n_dollar   NUMBER;
   n_cents    NUMBER;

   FUNCTION check_if_single (i_num IN NUMBER, currency IN VARCHAR2)
      RETURN VARCHAR2
   IS
      FUNCTION n_spell (i_num IN NUMBER)
         RETURN VARCHAR2
      AS
         TYPE w_Array IS TABLE OF VARCHAR2 (255);

         l_str w_array
               := w_array ('',
                           ' thousand ',
                           ' million ',
                           ' billion ',
                           ' trillion ',
                           ' quadrillion ',
                           ' quintillion ',
                           ' sextillion ',
                           ' septillion ',
                           ' octillion ',
                           ' nonillion ',
                           ' decillion ',
                           ' undecillion ',
                           ' duodecillion ');

         l_num           VARCHAR2 (50) DEFAULT TRUNC (i_num);
         l_is_negative   BOOLEAN := FALSE;
         l_return        VARCHAR2 (4000);
      BEGIN
         IF SIGN (i_num) = -1
         THEN
            l_is_negative := TRUE;
            l_num := TRUNC (ABS (i_num));
         END IF;

         FOR i IN 1 .. l_str.COUNT
         LOOP
            EXIT WHEN l_num IS NULL;

            IF (SUBSTR (l_num, LENGTH (l_num) - 2, 3) <> 0)
            THEN
               l_return :=
                  TO_CHAR (
                     TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
                     'Jsp')
                  || l_str (i)
                  || l_return;
            END IF;

            l_num := SUBSTR (l_num, 1, LENGTH (l_num) - 3);
         END LOOP;

         IF NOT l_is_negative
         THEN
            RETURN INITCAP (l_return);
         ELSE
            RETURN 'Negative ' || INITCAP (l_return);
         END IF;
      END n_spell;
   BEGIN
      IF i_num = 1
      THEN
         RETURN 'One ' || currency;
      ELSE
         RETURN n_spell (i_num) || ' ' || currency;
      END IF;
   END check_if_single;
BEGIN
   IF i_amt IS NULL
   THEN
      RETURN '';
   END IF;

   n_dollar := TRUNC (i_amt);
   n_cents := (ABS (i_amt) - TRUNC (ABS (i_amt))) * 100;

   IF NVL (n_cents, 0) > 0
   THEN
      RETURN    check_if_single (n_dollar, 'Dollar')
             || ' and '
             || check_if_single (n_cents, 'Cents');
   ELSE
      RETURN check_if_single (n_dollar, 'Dollar');
   END IF;
END amount_in_words;
/
Comment

PREVIOUS NEXT
Code Example
Sql :: how to add session data into mysql database from button 
Sql :: print intervals of 15 minutes in sql query 
Sql :: redshift alter table alter column set not null 
Sql :: copy row from db to db mysql 
Sql :: like in openquery 
Sql :: enter postgres shell o localhost 
Sql :: mysql could not fetch tables 
Sql :: sql insert into only auto id 
Sql :: mysql convert charset 
Sql :: mostrar datos de tablas relacionadas mysql kjava 
Sql :: PROSYS SQL 
Sql :: oracle convert run_duration to number 
Sql :: extract domain name from email id mariadb 
Sql :: sqdqsd 
Sql :: Create a username nd password in MySql 
Sql :: oracle rolling back 
Sql :: BSD sed: extra characters at the end of d command 
Sql :: group by join columns per table 
Sql :: Pattern Sql Rlike same as REGEXP 
Sql :: sql oop example 
Sql :: get last query in codeigniter 4 
Sql :: install package for sqlserver in asp.net core 
Sql :: select all fields from table 
Sql :: django mysqlclient connection string 
Sql :: Monthly Birthday SQL Query 
Sql :: mysql primary vs unique 
Sql :: power bi dax add related shows column but not working 
Sql :: Get the First and Last Word from a String or Sentence 
Sql :: oracle factorial 
Sql :: codeigniter MySQL - Issue with SELECT & GROUP BY 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =