Sunday, March 13, 2011

PL/SQL Function to conver amount in number value to words

FUNCTION test_amt_in_word (in_val NUMBER, curr VARCHAR2, subunit VARCHAR2)
RETURN VARCHAR2
IS
billion NUMBER (15);
million NUMBER (12);
thousand NUMBER (9);
paisas NUMBER (6);
spell_amt VARCHAR2 (160);
positive_val NUMBER (15, 2);
BEGIN
IF in_val < 0
THEN
positive_val := in_val * -1;
ELSE
positive_val := in_val;
END IF;
SELECT TO_NUMBER (SUBSTR (LPAD ((positive_val - MOD (positive_val, 1)),
15,
'0'
),
-12,
3
)
),
TO_NUMBER (SUBSTR (LPAD ((positive_val - MOD (positive_val, 1)),
15,
'0'
),
-9,
3
)
),
TO_NUMBER (SUBSTR (LPAD ((positive_val - MOD (positive_val, 1)),
15,
'0'
),
-6,
6
)
),
TO_NUMBER (RPAD (SUBSTR (MOD (positive_val, 1), 2, 2), 2, '0'))
INTO billion,
million,
thousand,
paisas
FROM DUAL;
IF NVL (billion, 0) != 0
THEN
SELECT INITCAP (TO_CHAR (TO_DATE (billion, 'j'), 'jsp')) || ' Billion'
INTO spell_amt
FROM DUAL;
END IF;
IF NVL (million, 0) != 0
THEN
SELECT RTRIM (spell_amt)
|| ' '
|| INITCAP (TO_CHAR (TO_DATE (million, 'j'), 'jsp'))
|| ' Million'
INTO spell_amt
FROM DUAL;
END IF;
IF NVL(thousand, 0) != 0
THEN
SELECT RTRIM (spell_amt)
|| ' '
|| INITCAP (TO_CHAR (TO_DATE (thousand, 'j'), 'jsp'))
|| ' '
|| curr
INTO spell_amt
FROM DUAL;
END IF;
IF NVL(paisas, 0) != 0
THEN
SELECT RTRIM (spell_amt)
|| '  '
||'and '
|| INITCAP (TO_CHAR (TO_DATE (paisas, 'j'), 'jsp'))||' '||subunit
INTO spell_amt
FROM DUAL;
END IF;
RETURN spell_amt;
END  test_amt_in_word ;

No comments:

Post a Comment