復制代碼
代碼如下:
--函數
CREATE
function
fn_GetPy(@str
nvarchar(4000))
returns
nvarchar(4000)
--WITH
ENCRYPTION
as
begin
declare
@intLenint
declare
@strRetnvarchar(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
go
--調用
select
dbo.fn_getpy('張三')
--返回:zs
答!:
2:
取漢字拼音首字母的存儲過程
Create
function
fun_getPY
(
@str
nvarchar(4000)
)
returns
nvarchar(4000)
as
begin
declare
@word
nchar(1),@PY
nvarchar(4000)
set
@PY=''
while
len(@str)>0
begin
set
@word=left(@str,1)
--如果非漢字字元,返回原字元
set
@PY=@PY+(case
when
unicode(@word)
between
19968
and
19968+20901
then
(
select
top
1
PY
from
(
select
'A'
as
PY,N'驁'
as
word
union
all
select
'B',N'簿'
union
all
select
'C',N'錯'
union
all
select
'D',N'鵽'
union
all
select
'E',N'樲'
union
all
select
'F',N'鰒'
union
all
select
'G',N'腂'
union
all
select
'H',N'夻'
union
all
select
'J',N'攈'
union
all
select
'K',N'穒'
union
all
select
'L',N'鱳'
union
all
select
'M',N'旀'
union
all
select
'N',N'桛'
union
all
select
'O',N'漚'
union
all
select
'P',N'曝'
union
all
select
'Q',N'囕'
union
all
select
'R',N'鶸'
union
all
select
'S',N'蜶'
union
all
select
'T',N'籜'
union
all
select
'W',N'鶩'
union
all
select
'X',N'鑂'
union
all
select
'Y',N'韻'
union
all
select
'Z',N'咗'
)
T
where
word>=@word
collate
Chinese_PRC_CS_AS_KS_WS
order
by
PY
ASC
)
else
@word
end)
set
@str=right(@str,len(@str)-1)
end
return
@PY
end
Ⅱ sql server根據名字生成拼音,怎麼做
sql server自動生成拼音首字母的函數
http://blog.csdn.net/luckeryin/article/details/3377378
Ⅲ SQL SERVER 獲取字元串拼音函數,求優化
按你這種寫法看起來優化程度有限。可以考慮吧substring改成left。每次循環完把第一個字元去掉。可能比substring 快點。
另外有個思路。就是需要替換成字母的字已經是確認的了。我數了一下是23個字
那你循環23遍 吧對應的字替換成對應的字母。 這樣循環次數就固定下來,應該比傳進來的字元串一個個循環次數少 。效率肯定會高!
Ⅳ sql 漢字轉拼音,若有重復的在拼音加1
題主意思,表a已經有欄位hz,現在要更新py這列?這樣的話,只要update就行了,為什麼說最好是函數實現呢?還是說題主想要selecthz,func(hz)froma這樣的呢?
函數。
首先需要一個解析漢字到拼音的方法,這里有一個包,你可以復制過去直接執行。
腳本地址:
http://www.cnblogs.com/mellowsmile/p/4601288.html
附代碼:
--不是我不貼,代碼太多貼不上,題主還是去上面那個地址復制吧,注意復制全部。
在SQL窗口執行。
我這里建了一個測試表test,數據顯示如下:
select a.* from test a;
題主,如此,可否?
Ⅳ SQL語句提取出中文的拼音首字母
正好最近收藏了一個 你可以看下思路
--將中文字元串轉化成文字首拼音的組合
create function fun_getPY(@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非漢字字元,返回原字元
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (select top 1 PY from (
select 'A' as PY,N'驁' as word
union all select 'B',N'簿'
union all select 'C',N'錯'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鰒'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'漚'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'籜'
union all select 'W',N'鶩'
union all select 'X',N'鑂'
union all select 'Y',N'韻'
union all select 'Z',N'咗'
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC) else @word end)
set @str=right(@str,len(@str)-1)
end
return @PY
end
--函數調用實例:
select dbo.fun_getPY('中華人民共和國AAA01')
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZHRMGHGAAA01
(1 行受影響)
*/
Ⅵ sql批量生成拼音碼問題
1
創建這個函數。
Create
function fun_getPY
(
@str
nvarchar(4000)
)
returns
nvarchar(4000)
as
begin
declare
@word nchar(1),@PY nvarchar(4000)
set
@PY=''
while
len(@str)>0
begin
set
@word=left(@str,1)
--
set
@PY = @PY+(casewhenunicode(@word)between 19968 and 19968+20901
then (
select
top 1 PY
from
(
select
'A'as PY,N''as word
union
allselect'B',N''
union
allselect'C',N''
union
allselect'D',N''
union
allselect'E',N''
union
allselect'F',N''
union
allselect'G',N''
union
allselect'H',N''
union
allselect'J',N''
union
allselect'K',N''
union
allselect'L',N''
union
allselect'M',N''
union
allselect'N',N''
union
allselect'O',N''
union
allselect'P',N''
union
allselect'Q',N''
union
allselect'R',N''
union
allselect'S',N''
union
allselect'T',N''
union
allselect'W',N''
union
allselect'X',N''
union
allselect'Y',N''
union
allselect'Z',N''
)
T
where
word>=@word collate Chinese_PRC_CS_AS_KS_WS
order
by PY ASC
)
else
@word
end
)
set
@str=right(@str,len(@str)-1)
end
return
@PY
end
2
select dbo.fun_getPY(該欄位) from 表
Ⅶ 如果我要加一些數據的拼音進資料庫,SQL語句怎麼寫啊
sql函數返回拼音碼/五筆碼/自定義碼
2009-02-23 09:27
if exists(select * from sysobjects where name='fun_get_bm') drop function fun_get_bm
go
create function fun_get_bm(@str nvarchar(4000),@srm varchar(20))
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非漢字字元,返回原字元
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (select top 1 PY from (select top 1 case
when @srm='1' then srm1
when @srm='2' then srm2
else srm3
end as PY,word from xt_hzk where word=@word
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC) else @word end)
set @str=right(@str,len(@str)-1)
end
return replace(replace(@PY,' ',''),'$','')
end
go
--xt_hzk這個表是我們自己寫的漢字型檔表
--用法如
select dbo.fun_get_bm('張三豐','1')
--參數第一個編譯的值,第二個傳遞1[拼音碼],2[五筆碼],3[自定義碼]
輸出 ZSF
貼過來的 參考 呵呵
Ⅷ 漢字轉拼音的sql函數為什麼用到這些字 :驁簿錯鵽樲鰒腂夻攈穒鱳旀桛漚曝囕鶸蜶籜鶩鑂韻咗,求原理
沒人回答,自己找到了答案,整個sql排序規則是按照拼音排序, 以此句sql為例:'A' AS PY,N'驁' AS word 」驁「字代表的是以A開頭的拼音,並且音調是也要排最後的一個字,讀做「ào」,所以得出在這個區間的漢字首字母都為A,以此類推可查出其他字母開頭的漢字首字母
Ⅸ 如何用db2的sql寫一個獲取漢字拼音首字母的
DECLARE @str VARCHAR(100)
SET @str = '漢字的首字母'
SELECT @str AS A, dbo.fun_getPY(@str) AS B
先執行上面的那個函數,然後在執行下面的那個語句,就可以得到你要的結果了。
Ⅹ sql中怎樣按拼音排序
如果只是英文和數字的可以試試下面的方法:
在ORACLE SQL中有這樣一個函數ASCII(char)
ASCII(char)的參數可以為字元串
例如:
select ASCII('D'), ASCII('Dave') FROM al
返回為:
ASCII('D') ASCII('Dave')
---------------------------
68 68
不知道這些對你有幫助沒