根據漢字的首字母生產拼音碼進行查詢

2021-08-13 05:03:32 字數 3795 閱讀 7090

主要是在sql中實現

use

[xpos_refer_database]go

/****** object: userdefinedfunction [dbo].[getletter] script date: 04/07/2012 09:22:25 *****

*/set ansi_nulls on

goset quoted_identifier on

goalter

function

[dbo

].[getletter

](@str

nvarchar(4000),@type

int)

returns

nvarchar(4000)

--with encryption

asbegin

declare

@intlen

int--

長度declare

@strret

nvarchar(4000)--

返回值declare

@temp

nvarchar(100) --

臨時變數

set@intlen

=len(@str)

set@strret=''

declare

@iint

--臨時變數

select@i=

1

while (@intlen

>

0and

@type

>

0)

begin

set@temp=''

select

@temp

=case

when

substring(@str,@i,1) >='

帀 'then'z

'when

substring(@str,@i,1) >='

丫 'then'y

'when

substring(@str,@i,1) >='

夕 'then'x

'when

substring(@str,@i,1) >='

屲 'then'w

'when

substring(@str,@i,1) >='

他 'then't

'when

substring(@str,@i,1) >='

仨 'then's

'when

substring(@str,@i,1) >='

呥 'then'r

'when

substring(@str,@i,1) >='

七 'then'q

'when

substring(@str,@i,1) >='

妑 'then'p

'when

substring(@str,@i,1) >='

噢 'then'o

'when

substring(@str,@i,1) >='

拏 'then'n

'when

substring(@str,@i,1) >='

嘸 'then'm

'when

substring(@str,@i,1) >='

垃 'then'l

'when

substring(@str,@i,1) >='

咔 'then'k

'when

substring(@str,@i,1) >='

丌 'then'j

'when

substring(@str,@i,1) >='

鉿 'then'h

'when

substring(@str,@i,1) >='

旮 'then'g

'when

substring(@str,@i,1) >='

發 'then'f

'when

substring(@str,@i,1) >='

妸 'then'e

'when

substring(@str,@i,1) >='

咑 'then'd

'when

substring(@str,@i,1) >='

嚓 'then'c

'when

substring(@str,@i,1) >='

八 'then'b

'when

substring(@str,@i,1) >='

吖 'then'a

'else

rtrim(ltrim(substring(@str,@i,1)))

end--對於漢字特殊字元,不生成拼音碼

if (ascii(@temp)>

127) set

@temp='

'--對於英文中小括號,不生成拼音碼

if@temp='

('or@temp='

)'set@temp='

'select

@strret

=@strret

+@temp

set@intlen

=@intlen

-1

if@type

<=

10set

@type

=@type

-1

set@i=

@i+1

end--

while

return

lower(@strret)

end

這個函式是在sql中「標量值函式」下面的

查詢的sql語句:

select

distinct d_city as 城市,upper(dbo.getletter(d_city,1)) as 拼音碼 from ref_addr3 order

byupper(dbo.getletter(d_city,1))

查詢結果:

根據漢字的首字母生產拼音碼進行查詢

公司裡原先的 實在沒看懂。主要是在sql中實現 use xpos refer database go object userdefinedfunction dbo getletter script date 04 07 2012 09 22 25 set ansi nulls on goset qu...

實現漢字拼音碼首字母的動態庫

實現漢字拼音碼首字母查詢的方法大致有如下幾種 1 根據漢字內碼碼查詢,2 根據微軟拼音輸入法查詢,3 自建拼音碼資料庫。本文僅簡單列出第一種方式的原始碼 其中主要原始碼來自網上,僅為了做成動態庫供其它語言呼叫略作修改,在此對作者表示感謝!檔名 py.dpr 將所有內容複製到記事本裡,然後另存為py....

漢字轉拼音首字母

public string hz2py string hz 獲得漢字的區位碼 else if tmp 45253 tmp 45760 else if tmp 47761 tmp 46317 else if tmp 46318 tmp 46825 else if tmp 46826 tmp 47009...