Wednesday, July 27, 2011

Oracle - local Currency In Word By Khurram

















CREATE OR REPLACE FUNCTION Rupees_In_Word (Rupees IN NUMBER) RETURN VARCHAR2

/*------------------------------------------------------------------------
Author            Khurram Altaf
Created On        July 27, 2011
Trigger           Ruppee_In_Word
Description       Generates a Word Expression against Number
------------------------------------------------------------------------*/

AS
   v_rupee        VARCHAR2 (4000) := NULL;
   v_paisa        VARCHAR2 (4000) := NULL;
   p_amount       VARCHAR2 (50)   := TO_CHAR (TRUNC (Rupees));
   v_point        VARCHAR2 (50)   := NULL;
  


   TYPE Rupee_Array IS TABLE OF VARCHAR2 (255);

   v_str_arry      Rupee_Array         := Rupee_Array (' Thousand ', ' Lakh ', ' Crore ');
BEGIN
   IF ((Rupees = 0) OR (Rupees IS NULL))
   THEN
      v_rupee := 'zero';
   ELSIF (TO_CHAR (Rupees) LIKE '%.%')
   THEN
      IF (SUBSTR (Rupees, INSTR (Rupees, '.') + 1) > 0)
      THEN
         v_point := SUBSTR (Rupees, INSTR (Rupees, '.') + 1);

         IF (LENGTH (v_point) < 2)
         THEN
            v_point := v_point * 10;
         END IF;

         v_paisa :=
               ' AND '
            || (TO_CHAR (TO_DATE (SUBSTR (v_point, LENGTH (v_point) - 1, 2),
                                  'J'),
                         'JSP'
                        )
               )
            || ' paise ';
         p_amount := SUBSTR (Rupees, 1, INSTR (Rupees, '.') - 1);
         v_rupee :=
               TO_CHAR (TO_DATE (SUBSTR (p_amount, LENGTH (p_amount) - 2, 3),
                                 'J'
                                ),
                        'Jsp'
                       )
            || v_rupee;
         p_amount := SUBSTR (p_amount, 1, LENGTH (p_amount) - 3);

         FOR i IN 1 .. v_str_arry.COUNT
         LOOP
            EXIT WHEN (p_amount IS NULL);
            v_rupee :=
                  TO_CHAR (TO_DATE (SUBSTR (p_amount, LENGTH (p_amount) - 1,
                                            2),
                                    'J'
                                   ),
                           'Jsp'
                          )
               || v_str_arry (i)
               || v_rupee;
            p_amount := SUBSTR (p_amount, 1, LENGTH (p_amount) - 2);
         END LOOP;
      END IF;
   ELSE
      v_rupee := TO_CHAR (TO_DATE (TO_CHAR (Rupees, '999999999'), 'J'), 'JSP');
   END IF;

   v_rupee := v_rupee || ' ' || v_paisa || ' only ';
   v_rupee := REPLACE (RTRIM (v_rupee), ' ', ' ');
   v_rupee := REPLACE (RTRIM (v_rupee), '-', ' ');
   RETURN INITCAP (v_rupee);
END Rupees_In_Word;

-------------------------------------------------------------

select Rupees_In_Word (193246.65) from dual;

-------------------------------------------------------------

No comments:

Post a Comment