如何在Oracle中實現人民幣大寫的轉換
作者: CCBZZP
ORACLE在實現報表的功能是很強大的, 特別在現實的應用中會經常用到人民幣大小寫轉換的問題, 在此我寫個函數向大家簡單介紹一下, 希望和大家一起探討, 以便共同進步! 共同發展!
1. 函數DX_MONEY()
CREATE FUNCTION DX_MONEY
(
MONEY IN NUMBER
)
RETURN VARCHAR2 AS V_MONEY VARCHAR2(150);
RV_MONEY0 VARCHAR2(20);
RV_MONEY1 VARCHAR2(4);
RV_MONEY2 VARCHAR2(4);
V_MONEY0 VARCHAR2(20);
V_MONEY1 VARCHAR2(4);
V_MONEY2 VARCHAR2(4);
V_MONEY3 VARCHAR2(4);
V_MONEY4 VARCHAR2(4);
V_MONEY5 VARCHAR2(4);
V_MONEY6 VARCHAR2(4);
V_MONEY7 VARCHAR2(4);
V_MONEY8 VARCHAR2(4);
V_MONEY9 VARCHAR2(4);
V_MONEY10 VARCHAR2(4);
V_MONEY11 VARCHAR2(4);
V_MONEY12 VARCHAR2(4);
RPV_MONEY1 VARCHAR2(4);
RPV_MONEY2 VARCHAR2(4);
PV_MONEY0 VARCHAR2(14);
PV_MONEY1 VARCHAR2(4);
PV_MONEY2 VARCHAR2(4);
PV_MONEY3 VARCHAR2(4);
PV_MONEY4 VARCHAR2(4);
PV_MONEY5 VARCHAR2(4);
PV_MONEY6 VARCHAR2(4);
PV_MONEY7 VARCHAR2(4);
PV_MONEY8 VARCHAR2(4);
PV_MONEY9 VARCHAR2(4);
PV_MONEY10 VARCHAR2(4);
PV_MONEY11 VARCHAR2(4);
PV_MONEY12 VARCHAR2(4);
BEGIN
V_MONEY0:=LTRIM(RTRIM(TO_CHAR(FLOOR(MONEY))));
RV_MONEY0:=LTRIM(RTRIM(TO_CHAR(FLOOR(MONEY*100))));
RV_MONEY1:=SUBSTR(RV_MONEY0,LENGTH(RV_MONEY0),1);
IF LENGTH(RV_MONEY0)>1 THEN
RV_MONEY2:=SUBSTR(RV_MONEY0,LENGTH(RV_MONEY0)-1,1);
ELSE
RV_MONEY2:='0';
END IF;
V_MONEY1:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0),1);
IF LENGTH(V_MONEY0)-1>0 THEN
V_MONEY2:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-1,1);
ELSE V_MONEY2:=0;
END IF;
IF LENGTH(V_MONEY0)-2>0 THEN
V_MONEY3:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-2,1);
ELSE V_MONEY3:=0;
END IF;
IF LENGTH(V_MONEY0)-3>0 THEN
V_MONEY4:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-3,1);
ELSE V_MONEY4:=0;
END IF;
IF LENGTH(V_MONEY0)-4>0 THEN
V_MONEY5:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-4,1);
ELSE V_MONEY5:=0;
END IF;
IF LENGTH(V_MONEY0)-5>0 THEN
V_MONEY6:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-5,1);
ELSE V_MONEY6:=0;
END IF;
IF LENGTH(V_MONEY0)-6>0 THEN
V_MONEY7:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-6,1);
ELSE V_MONEY7:=0;
END IF;
IF LENGTH(V_MONEY0)-7>0 THEN
V_MONEY8:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-7,1);
ELSE V_MONEY8:=0;
END IF;
IF LENGTH(V_MONEY0)-8>0 THEN
V_MONEY9:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-8,1);
ELSE V_MONEY9:=0;
END IF;
IF LENGTH(V_MONEY0)-9>0 THEN
V_MONEY10:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-9,1);
ELSE V_MONEY10:=0;
END IF;
IF LENGTH(V_MONEY0)-10>0 THEN
V_MONEY11:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-10,1);
ELSE V_MONEY11:=0;
END IF;
IF LENGTH(V_MONEY0)-11>0 THEN
V_MONEY12:=SUBSTR(V_MONEY0,LENGTH(V_MONEY0)-11,1);
ELSE V_MONEY12:=0;
END IF;
select DECODE(RV_MONEY1,'0','零分','1','壹分','2','貳分','3','參分','4','肆分','5','伍分',
'6','陸分','7','柒分','8','扒分','9','玖分') into rpv_money1 from dual;
select DECODE(RV_MONEY2,'0','零角','1','壹角','2','貳角','3','參角','4','肆角','5','伍角',
'6','陸角','7','柒角','8','扒角','9','玖角') into rpv_money2 from dual;
select DECODE(V_MONEY1,'0','元','1','壹元','2','貳元','3','參元','4','肆元','5','伍元',
'6','陸元','7','柒元','8','扒元','9','玖元') into pv_money1 from dual;
select DECODE(V_MONEY2,'0','','1','壹拾','2','貳拾','3','參拾','4','肆拾','5','伍拾',
'6','陸拾','7','柒拾','8','扒拾','9','玖拾') into pv_money2 from dual;
select DECODE(V_MONEY3,'0','','1','壹佰','2','貳佰','3','參佰','4','肆佰','5','伍佰',
'6','陸佰','7','柒佰','8','扒佰','9','玖佰') into pv_money3 from dual;
select DECODE(V_MONEY4,'0','','1','壹仟','2','貳仟','3','參仟','4','肆仟','5','伍仟',
'6','陸仟','7','柒仟','8','扒仟','9','玖仟') into pv_money4 from dual;
select DECODE(V_MONEY5,'0','萬','1','壹萬','2','貳萬','3','參萬','4','肆萬','5','伍萬',
'6','陸萬','7','柒萬','8','扒萬','9','玖萬') into pv_money5 from dual;
select DECODE(V_MONEY6,'0','','1','壹拾','2','貳拾','3','參拾','4','肆拾','5','伍拾',
'6','陸拾','7','柒拾','8','扒拾','9','玖拾') into pv_money6 from dual;
select DECODE(V_MONEY7,'0','','1','壹佰','2','貳佰','3','參佰','4','肆佰','5','伍佰',
'6','陸佰','7','柒佰','8','扒佰','9','玖佰') into pv_money7 from dual;
select DECODE(V_MONEY8,'0','','1','壹仟','2','貳仟','3','參仟','4','肆仟','5','伍仟',
'6','陸仟','7','柒仟','8','扒仟','9','玖仟') into pv_money8 from dual;
select DECODE(V_MONEY9,'0','億','1','壹億','2','貳億','3','參億','4','肆億','5','伍億',
'6','陸億','7','柒億','8','扒億','9','玖億') into pv_money9 from dual;
select DECODE(V_MONEY10,'0','','1','壹拾','2','貳拾','3','參拾','4','肆拾','5','伍拾',
'6','陸拾','7','柒拾','8','扒拾','9','玖拾') into pv_money10 from dual;
select DECODE(V_MONEY11,'0','','1','壹佰','2','貳佰','3','參佰','4','肆佰','5','伍佰',
'6','陸佰','7','柒佰','8','扒佰','9','玖佰') into pv_money11 from dual;
select DECODE(V_MONEY12,'0','','1','壹仟','2','貳仟','3','參仟','4','肆仟','5','伍仟',
'6','陸仟','7','柒仟','8','扒仟','9','玖仟') into pv_money12 from dual;
V_MONEY:=PV_MONEY12PV_MONEY11PV_MONEY10PV_MONEY9PV_MONEY8
PV_MONEY7PV_MONEY6PV_MONEY5PV_MONEY4PV_MONEY3PV_MONEY2PV_MONEY1
RPV_MONEY2RPV_MONEY1;
RETURN (V_MONEY);
END;
2. 執行狀況如下:
SQL*Plus: Release 9.2.0.1.0 - ProdUCtion on 星期二 7月 29 15:03:36 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
連線到:
Oracle8i Enterprise Edition Release 8.1.6.1.0 - Production
With the Partitioning option
JServer Release 8.1.6.0.0 - Production
SQL> select dx_money(324450567.89) from dual;
DX_MONEY(324450567.89)
--------------------------------------------------------------------------------
參億貳仟肆佰肆拾伍萬伍佰陸拾柒元扒角玖分
3. 本例的最大數字999999999999.99
4. 以上函數寫的比較簡單易懂, 語法上不一定精簡, 歡迎大家一起探討!