Convert Number to Words Using PL/SQL Function
/* Formatted on 2013/03/07 18:40 (Formatter Plus v4.8.8) */
CREATE OR REPLACE FUNCTION spell_number (p_amt IN NUMBER, POH_CURRENCY_CODE in varchar2)
RETURN VARCHAR2
IS
m_main_amt_text VARCHAR2 (2000);
m_top_amt_text VARCHAR2 (2000);
m_bottom_amt_text VARCHAR2 (2000);
m_decimal_text VARCHAR2 (2000);
m_top NUMBER (20, 5);
m_main_amt NUMBER (20, 5);
m_top_amt NUMBER (20, 5);
m_bottom_amt NUMBER (20, 5);
m_decimal NUMBER (20, 5);
m_amt NUMBER (20, 5);
m_text VARCHAR2 (2000);
r_val VARCHAR2 (10) := 'Dollar ';
v_curr varchar2(10);
BEGIN
m_main_amt := NULL;
m_top_amt_text := NULL;
m_bottom_amt_text := NULL;
m_decimal_text := NULL;
-- To get paise part
m_decimal := p_amt - TRUNC (p_amt);
IF m_decimal > 0
THEN
m_decimal := m_decimal * 100;
END IF;
m_amt := TRUNC (p_amt);
m_top := TRUNC (m_amt / 100000);
m_main_amt := TRUNC (m_top / 100);
m_top_amt := m_top - m_main_amt * 100;
m_bottom_amt := m_amt - (m_top * 100000);
m_top_amt_text := TO_CHAR (TO_DATE (m_amt, 'J'), 'JSP');
/*IF m_main_amt > 0
THEN
m_main_amt_text := TO_CHAR (TO_DATE (m_amt, 'J'), 'JSP');
/* IF m_main_amt = 1
THEN
m_main_amt_text := m_main_amt_text || ' CRORE ';
ELSE
m_main_amt_text := m_main_amt_text || ' CRORES ';
END IF;
END IF;
IF m_top_amt > 0
THEN
m_top_amt_text := TO_CHAR (TO_DATE (m_amt, 'J'), 'JSP');
IF m_top_amt = 1
THEN
m_top_amt_text := m_top_amt_text || ' LAKH ';
ELSE
m_top_amt_text := m_top_amt_text || ' LAKHS ';
END IF;
END IF;*/
IF m_bottom_amt > 0
THEN
m_bottom_amt_text := TO_CHAR (TO_DATE (m_bottom_amt, 'J'), 'JSP')
|| ' ';
END IF;
IF m_decimal > 0
THEN
IF NVL (m_bottom_amt, 0) + NVL (m_top_amt, 0) > 0
THEN
-- srw.message(111,'m_bottom_amt is '||m_bottom_amt);
-- srw.message(111,'m_top_amt is '||m_top_amt);
BEGIN
if (POH_CURRENCY_CODE = 'EUR') then
v_curr := 'Cents';
elsif (POH_CURRENCY_CODE = 'USD') then
v_curr := 'Cents';
elsif (POH_CURRENCY_CODE = 'OMR') then
v_curr := 'Baisa';
elsif (POH_CURRENCY_CODE = 'AED') then
v_curr := 'Fils';
else
v_curr := 'Paisa';
end if;
--m_decimal_text := ' AND ' || TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP')|| ' Cent ';
m_decimal_text := ' AND ' ||' '||v_curr||' '|| TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP');
END;
ELSE
if (POH_CURRENCY_CODE = 'EUR') then
v_curr := 'Cents';
elsif (POH_CURRENCY_CODE = 'USD') then
v_curr := 'Cents';
elsif (POH_CURRENCY_CODE = 'OMR') then
v_curr := 'Baisa';
elsif (POH_CURRENCY_CODE = 'AED') then
v_curr := 'Fils';
else
v_curr := 'Paisa';
end if;
--m_decimal_text := TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP') || ' Cent ';
m_decimal_text := 'AND'||' '||v_curr||' '|| TO_CHAR (TO_DATE (m_decimal, 'J'), 'JSP');
r_val := '';
END IF;
END IF;
m_text :=
-- r_val
-- || LOWER ( m_main_amt_text
(m_top_amt_text
|| m_decimal_text
)
|| ' ONLY';
-- dbms_output.put_line('m_text is '||m_text);
-- m_text := UPPER (SUBSTR (m_text, 1, 1)) || SUBSTR (m_text, 2); 252345.36
-- m_text := SUBSTR (m_text, 2);
-- m_text := ' ' || m_text;
RETURN (REPLACE(UPPER(m_text),'-',' '));
END ;
Example:
SELECT spell_number(:AMOUNT,'USD') FROM DUAL;
==========================================================
/* Formatted on 10-01-2015 03:20:54 PM (QP5 v5.163.1008.3004) */
CREATE OR REPLACE FUNCTION MONEY_TO_WORDS (P_MONEY IN NUMBER)
RETURN VARCHAR2
AS
W_RETURN VARCHAR2 (255);
BEGIN
IF P_MONEY < 0
THEN
W_RETURN := 'Negative number is not allowed!';
ELSIF P_MONEY = 0
THEN
W_RETURN := 'Zero Euro and zero cents';
ELSE
IF (TRUNC (P_MONEY) = 0)
THEN
W_RETURN := 'Zero Euros';
ELSIF (TRUNC (P_MONEY) = 1)
THEN
W_RETURN := 'One Euro';
ELSE
W_RETURN := TO_CHAR (TO_DATE (TRUNC (P_MONEY), 'J'), 'JSP');
END IF;
IF ( (P_MONEY - TRUNC (P_MONEY)) = 0)
THEN
W_RETURN := W_RETURN || ' Exactly';
ELSIF ( (P_MONEY - TRUNC (P_MONEY)) * 100 = 1)
THEN
W_RETURN := W_RETURN || ' and one cent';
ELSE
W_RETURN :=
W_RETURN || 'and'
|| TO_CHAR (TO_DATE ( (P_MONEY - TRUNC (P_MONEY)) * 100, 'J'),
'JSP')
|| 'cents';
END IF;
END IF;
RETURN W_RETURN;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('Error : ' || SQLERRM);
RETURN 'CAN NOT CONVERT YOUR INPUT TO WORDS!';
END;
/
Example:
SELECT MONEY_TO_WORDS(0) FROM DUAL;
No comments:
Post a Comment