很有趣的,漢字轉換為其發音的首字母

rainbowbridg發表於2007-05-16
create or replace function csound(c in varchar2,

op in integer default 1

)

return
varchar2
/*

作者:chensq@itpub.net

日期:2005-05-18 22:30

說明:此函式用於將漢字轉換為其發音的首字母,對於ascii碼值在1~128間的字元不作任何處理,其它字元返回錯誤資訊。

用法:預設情況下,只對第一個字元進行轉換,如果加上不等於1的引數,則對全字串進行處理,支援中英文合編。如:

SQL> select csound('中國人用自己的sound()函式',2) from dual ;

CSOUND('中國人用自己的SOUND()

-------------------------------------------------------------

ZGRYZJDsound()HS



SQL> select csound('中國人用自己的sound()函式') from dual ;

CSOUND('中國人用自己的SOUND()

--------------------------------------------------------------
Z   



備註:此函式只在ZHS16GBK字符集的Oracle 9.2中透過,其它平臺沒有測試,請根據實際需要作相應的修改。

*/
is

c1 varchar2
(2);
c2 varchar2(2);
p integer;
n1 integer;
n2 integer;
csound varchar2(32767);
begin

if op=1 then p:=1 ;

else
p:=length(c);

end if;

for
i in 1..p

loop

c2
:=substr(c,i,1);

n1:=floor(ascii(c2)/256);

n2:=mod(ascii(c2),256);

if
n1=0 and n2<=129 then

c1
:=c2;

elsif n2<>127 and n2<>255

and not (n2 between 0 and 63) then

select

case

when c2>='丂' and c2<'芭' then 'A'
  when c2>='芭' and c2<'擦' then 'B'

when c2>='擦' and c2<'搭' then 'C'

when c2>='搭' and c2<'蛾' then 'D'

when c2>='蛾' and c2<'發' then 'E'

when c2>='發' and c2<'噶' then 'F'

when c2>='噶' and c2<'哈' then 'G'

when c2>='哈' and c2<'擊' then 'H'

when c2>='擊' and c2<'喀' then 'J'

when c2>='喀' and c2<'垃' then 'K'

when c2>='垃' and c2<'媽' then 'L'

when c2>='媽' and c2<'拿' then 'M'

when c2>='拿' and c2<'哦' then 'N'

when c2>='哦' and c2<'啪' then 'O'

when c2>='啪' and c2<'期' then 'P'

when c2>='期' and c2<'然' then 'Q'

when c2>='然' and c2<'撒' then 'R'

when c2>='撒' and c2<'塌' then 'S'

when c2>='塌' and c2<'挖' then 'T'

when c2>='挖' and c2<'稀' then 'W'

when c2>='稀' and c2<'壓' then 'X'

when c2>='壓' and c2<'匝' then 'Y'

when c2>='匝' and c2<='鼱' then 'Z' end

into c1

from dual
;

else

c1:='er';

end if;

csound:=csound||c1;

end loop;

return
csound;
end;


SQL> select csound('中國人用自己的sound()函式',2) from dual ;


CSOUND('中國人用自己的SOUND()

----------------------------------------------------------------



ZGRYZJDsound()HS



SQL> select csound('
臺海分離美國得利',2) from dual ;



CSOUND('
臺海分離美國得利',2)

---------------------------------------------------------------



THFLMGDL



SQL> select csound('
熱烈慶祝財富論壇圓滿召開',2) from dual ;



CSOUND('
熱烈慶祝財富論壇圓滿召
---------------------------------------------------------------


RLQZCFLTYMZK[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7916042/viewspace-915180/,如需轉載,請註明出處,否則將追究法律責任。

相關文章