最近做一个统计的报表系统,里面有关于罚款金额的统计,统计的数据来源于另外的一个系统,那个系统里面的金额居然都是大写的,大写的也就算了,关键是那些数值还是由人工去录入的,不是由程序转换的,于是呼出现了 一二三...十 壹贰叁...拾 123...9 各种表达方式,真不知道那个做系统的小伙子是不是刚毕业出来的。废话少说,便于统计,于是 花了一下午写了个 由 中文大写转换数值的函数(只转换元以上的,角、分没弄),经测试 尚未发现错误, 利人利己,在此贴出一起分享、测试。
create or replace function to_money(src in varchar2) --by zhang 2013-7-23
return number
is
v_source varchar2(128);
money number;
g varchar2(4);
w varchar2(4);
begin
v_source := src;
money := 0;
--空返回0
if(v_source is null) then
begin
money := 0;
return money;
end;
end if;
--数值的话,就直接返回
if(isnumber(v_source)) then
begin
money := to_number(v_source);
return money;
end;
end if;
--将 元、园、圆 及其后面的字符一并删掉
if v_source like '%元%' then
v_source := substr(v_source,0,instr(v_source,'元',-1)-1);
elsif v_source like '%园%' then
v_source := substr(v_source,0,instr(v_source,'园',-1)-1);
elsif v_source like '%圆%' then
v_source := substr(v_source,0,instr(v_source,'圆',-1)-1);
end if;
--将 一、二、三...九、十 转换为 壹、贰、叁...玖、拾
v_source := replace(replace(replace(replace(replace(v_source,'一','壹'),'二','贰'),'三','叁'),'四','肆'),'五','伍');
v_source := replace(replace(replace(replace(replace(v_source,'六','陆'),'七','柒'),'八','捌'),'九','玖'),'十','拾');
v_source := replace(replace(v_source,'百','佰'),'千','仟');
dbms_output.put_line(v_source);
--亿以上转换
if instr(v_source,'亿',-1)>0 then
begin
money := money + to_money(substr(v_source,0,instr(v_source,'亿',-1)-1))*100000000;
v_source := substr(v_source,instr(v_source,'亿',-1)+1);
end;
end if;
--万 到千万
if instr(v_source,'万',-1)>0 then
begin
money := money + to_money(substr(v_source,0,instr(v_source,'万',-1)-1))*10000;
v_source := substr(v_source,instr(v_source,'万',-1)+1);
end;
end if;
--万以下
--将零去掉
v_source := replace(v_source,'零','');
--拾位
if instr(v_source,'拾',-1)>0 then
begin
--先取个位
g := substr(v_source,instr(v_source,'拾',-1)+1,1);
if g is not null then
begin
g := replace(replace(replace(replace(replace(g,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
g := replace(replace(replace(replace(g,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+to_number(g);
end;
end if;
--取拾位
w := substr(v_source,instr(v_source,'拾',-1)-1,1);
if w is null or length(w) =0 or w ='拾' then --拾捌元 改为 壹拾捌元
w := '壹';
end if;
w := replace(replace(replace(replace(replace(w,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
w := replace(replace(replace(replace(w,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+(to_number(w)*10);
v_source := substr(v_source,0,instr(v_source,'拾',-1)-2);
end;
end if;
--佰位
if instr(v_source,'佰',-1)>0 then
begin
--先取个位
g := substr(v_source,instr(v_source,'佰',-1)+1,1);
if g is not null then
begin
g := replace(replace(replace(replace(replace(g,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
g := replace(replace(replace(replace(g,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+to_number(g);
end;
end if;
--取佰位
w := substr(v_source,instr(v_source,'佰',-1)-1,1);
if w is null or length(w) =0 or w ='佰' then --与拾位同理
w := '壹';
end if;
w := replace(replace(replace(replace(replace(w,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
w := replace(replace(replace(replace(w,'陆','6'),'柒','7'),'捌','8'),'玖','9');
dbms_output.put_line('佰:' || w);
money := money+(to_number(w)*100);
v_source := substr(v_source,0,instr(v_source,'佰',-1)-2);
end;
end if;
--仟位
if instr(v_source,'仟',-1)>0 then
begin
--先取个位
g := substr(v_source,instr(v_source,'仟',-1)+1,1);
if g is not null then
begin
g := replace(replace(replace(replace(replace(g,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
g := replace(replace(replace(replace(g,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+to_number(g);
end;
end if;
--取仟位
w := substr(v_source,instr(v_source,'仟',-1)-1,1);
if w is null or length(w) =0 or w ='仟' then --与拾位同理
w := '壹';
end if;
w := replace(replace(replace(replace(replace(w,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
w := replace(replace(replace(replace(w,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+(to_number(w)*1000);
v_source := substr(v_source,0,instr(v_source,'佰',-1)-2);
end;
end if;
if v_source is not null and length(v_source)>0 then
begin
g:=v_source;
g := replace(replace(replace(replace(replace(g,'壹','1'),'贰','2'),'叁','3'),'肆','4'),'伍','5');
g := replace(replace(replace(replace(g,'陆','6'),'柒','7'),'捌','8'),'玖','9');
money := money+to_number(g);
end;
end if;
return money;
end;