oracle 繁体转换为简体

今天公司要求做国际化,Struts2的国际化可以在配置文件里面进行配置,但是数据库里面存储的却无从下手,请教了一位同事,给了一个存储过程,可以大部分实现,貌似不错,介绍如下:

存储过程如下:

CREATE OR REPLACE FUNCTION GYIC_NEW.TRANS_CHINESE (p_inputStr IN VARCHAR2, p_inputType IN NUMBER) RETURN VARCHAR2
IS
v_ret VARCHAR2(300) := '';
i INT := 0;
v_str VARCHAR2(3);
v_count INT := 0;
/******************************************************************************
NAME: transform_chinese
PURPOSE:
PARAMETERS: p_inputType = 0 ==> simple chinese transform to tradition chinese
p_inputType = 1 ==> tradition chinese transform to simple chinese

REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 2012/6/5 1. Created this function.

NOTES:

Automatically available Auto Replace Keywords:
Object Name: transform_chinese
Sysdate: 2012/6/5
Date and Time: 2012/6/5, 上午 10:29:10, and 2012/6/5 上午 10:29:10
Username: (set in TOAD Options, Procedure Editor)
Table Name: (set in the "New PL/SQL Object" dialog)

******************************************************************************/
BEGIN
if p_inputStr is null or p_inputStr = '' then
return null;
end if;

if p_inputType = 0 then
for i in 1.. length(p_inputStr)
loop
select count(*) into v_count from chineseword where simpleword = substr(p_inputStr, i, 1);
if v_count > 0 then
select traditionword into v_str from chineseword where simpleword = substr(p_inputStr, i, 1);

v_ret := v_ret || v_str;
else
v_ret := v_ret || substr(p_inputStr, i, 1);
end if;
end loop;
else
for i in 1.. length(p_inputStr)
loop
select count(*) into v_count from chineseword where traditionword = substr(p_inputStr, i, 1);
if v_count > 0 then
select simpleword into v_str from chineseword where traditionword = substr(p_inputStr, i, 1);

v_ret := v_ret || v_str;
else
v_ret := v_ret || substr(p_inputStr, i, 1);
end if;
end loop;
end if;
RETURN v_ret;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END TRANS_CHINESE;

存在chineseword表,里面有两个字段,simpleword表示简体的字段,traditionword表示繁体的字段,有的繁体可能写出来,建表和插入的sql如下:

建表:

-- Create table
create table GYIC_NEW.CHINESEWORD
(
simpleword VARCHAR2(2000),
traditionword VARCHAR2(2000)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);

数据太大了,也不可以上传附件,就不上传了

使用方法如下:

首先找你要转换的表的字段,例如我这里是AA表下面name这个字段存储的是繁体,需要转换为简体:

update aa set name = trans_chinese(name, 1) ;

原文地址:https://www.cnblogs.com/yurujun/p/3591395.html