CREATE OR REPLACE FUNCTION FN_NUM2HAN(n_number NUMBER)
RETURN VARCHAR2 AS
n_unit NUMBER;
n_length NUMBER;
v_prev VARCHAR2(8);
v_digit VARCHAR2(8);
v_number VARCHAR2(150);
v_return VARCHAR2(150);
BEGIN
-- [ Usage ]
-- SELECT FN_NUM2HAN(125670000 ) FROM DUAL; -- 일억이천오백육십칠만
-- SELECT FN_NUM2HAN(100000000 ) FROM DUAL; -- 1억
-- SELECT FN_NUM2HAN(1000000000000 ) FROM DUAL; -- 1조
-- SELECT FN_NUM2HAN(1000000000000000 ) FROM DUAL; -- 1000조
-- SELECT FN_NUM2HAN(10000000000000000000) FROM DUAL; -- 1000경
IF n_number = 0 AND n_number < 0 THEN
RETURN '영';
END IF;
n_unit := 0;
v_number := LTRIM(TO_CHAR(n_number,'99999999999999999999')); -- 천경
n_length := LENGTH(v_number);
v_digit := NULL;
FOR i IN 1 .. n_length LOOP
v_prev := v_digit;
v_digit := SUBSTR(v_number, n_length-i+1, 1);
IF v_digit = '1' THEN v_digit := '일'; END IF;
IF v_digit = '2' THEN v_digit := '이'; END IF;
IF v_digit = '3' THEN v_digit := '삼'; END IF;
IF v_digit = '4' THEN v_digit := '사'; END IF;
IF v_digit = '5' THEN v_digit := '오'; END IF;
IF v_digit = '6' THEN v_digit := '육'; END IF;
IF v_digit = '7' THEN v_digit := '칠'; END IF;
IF v_digit = '8' THEN v_digit := '팔'; END IF;
IF v_digit = '9' THEN v_digit := '구'; END IF;
IF v_digit = '1' THEN v_digit := '일'; END IF;
IF v_digit = '0' THEN v_digit := NULL; END IF;
-- DBMS_OUTPUT.PUT_LINE(' DIGIT : [' || v_digit || ']');
-- DBMS_OUTPUT.PUT_LINE(' DEBUG : [' || i || '/' || i || ']');
IF v_digit IS NOT NULL THEN
IF MOD(i, 4) = 2 THEN
v_digit := v_digit || '십';
END IF;
IF MOD(i, 4) = 3 THEN
v_digit := v_digit || '백';
END IF;
IF MOD(i, 4) = 0 THEN
v_digit := v_digit || '천';
END IF;
END IF;
IF (v_digit IS NOT NULL AND i = 5) OR --
(v_digit IS NOT NULL AND v_prev IS NULL AND i > 5 AND i<9) THEN
v_digit := v_digit || '만';
END IF;
IF (v_digit IS NOT NULL AND i = 9) OR
(v_digit IS NOT NULL AND v_prev IS NULL AND i > 9 AND i<13) THEN
v_digit := v_digit || '억';
END IF;
IF (v_digit IS NOT NULL AND i =13) OR
(v_digit IS NOT NULL AND v_prev IS NULL AND i >13 AND i<17) THEN
v_digit := v_digit || '조';
END IF;
IF (v_digit IS NOT NULL AND i =17) OR
(v_digit IS NOT NULL AND v_prev IS NULL AND i >17 AND i<21) THEN
v_digit := v_digit || '경';
END IF;
IF v_digit IS NOT NULL THEN
v_return := v_digit || v_return;
END IF;
IF MOD(i,4) = 4 THEN
n_unit := n_unit + 1;
END IF;
END LOOP;
--DBMS_OUTPUT.PUT_LINE('[' || v_return || ']');
RETURN v_return;
EXCEPTION
WHEN OTHERS THEN v_return := SQLCODE;
RETURN v_return;
END;
/
SELECT FN_NUM2HAN(125670000 ) FROM DUAL;