我們在寫程式中,經常會用到sql中將某個名字轉成拼音,然後儲存在資料庫中某個欄位中,方便快捷查詢,所以有必要寫sql函式來定義一下,話補多說,直接上**:
create function fn_getpy(@str nvarchar(4000))
returns nvarchar(
4000
) --用於加密
--with encryption
asbegin
declare @intlen
intdeclare @strret nvarchar(
4000
) declare @temp nvarchar(
100)
set @intlen =len(@str)
set @strret = ''
while @intlen > 0
begin
set @temp = ''
select @temp = case
when substring(@str,@intlen,
1) >= '
帀' then 'z'
when substring(@str,@intlen,
1) >= '
丫' then 'y'
when substring(@str,@intlen,
1) >= '
夕' then 'x'
when substring(@str,@intlen,
1) >= '
屲' then 'w'
when substring(@str,@intlen,
1) >= '
他' then 't'
when substring(@str,@intlen,
1) >= '
仨' then 's'
when substring(@str,@intlen,
1) >= '
呥' then 'r'
when substring(@str,@intlen,
1) >= '
七' then 'q'
when substring(@str,@intlen,
1) >= '
妑' then 'p'
when substring(@str,@intlen,
1) >= '
噢' then 'o'
when substring(@str,@intlen,
1) >= '
拏' then 'n'
when substring(@str,@intlen,
1) >= '
嘸' then 'm'
when substring(@str,@intlen,
1) >= '
垃' then 'l'
when substring(@str,@intlen,
1) >= '
咔' then 'k'
when substring(@str,@intlen,
1) >= '
丌' then 'j'
when substring(@str,@intlen,
1) >= '
鉿' then 'h'
when substring(@str,@intlen,
1) >= '
旮' then 'g'
when substring(@str,@intlen,
1) >= '
發' then 'f'
when substring(@str,@intlen,
1) >= '
妸' then 'e'
when substring(@str,@intlen,
1) >= '
咑' then 'd'
when substring(@str,@intlen,
1) >= '
嚓' then 'c'
when substring(@str,@intlen,
1) >= '
八' then 'b'
when substring(@str,@intlen,
1) >= '
吖' then 'a'
else rtrim(ltrim(substring(@str,@intlen,1
)))
end
--對於漢字特殊字元,不生成拼音碼
if (ascii(@temp)>127) set @temp = ''
--對於英文中小括號,不生成拼音碼
if @temp = '
(' or @temp = ')'
set @temp = ''
select @strret = @temp +@strret
set @intlen = @intlen - 1
end
return
lower(@strret)
end
呼叫方式:
select dbo.fn_getpy('張三丰') py
結果如下:
pyzsf
sql 函式 漢字轉拼音
go object userdefinedfunction dbo fn getpy script date 2017 1 4 10 53 49 set ansi nulls on goset quoted identifier on goalter function dbo fn getpy st...
sql 函式 漢字轉拼音
go object userdefinedfunction dbo fn getpy script date 2017 1 4 10 53 49 set ansi nulls on goset quoted identifier on goalter function dbo fn getpy st...
sql函式 漢字拼音碼
create or replace function to piny p name in varchar2 return varchar2 as v compare varchar2 100 v return varchar2 4000 function f nlssort p word in va...