SQL獲取拼音首字函式

2021-04-27 08:09:35 字數 2182 閱讀 5838

--

>建立函式:

create

function

f_getpy(

@str

nvarchar

(4000

)) returns

nvarchar

(4000

) as

begin

declare

@strlen

int,

@renvarchar

(4000

) declare

@ttable

(chr  

nchar(1

)   collate   chinese_prc_ci_as,letter  

nchar(1

)) insert

into

@t(chr,letter)

select'吖

', 'a

'union

allselect'八

', 'b

'union

allselect'嚓

', 'c

'union

allselect'咑

', 'd

'union

allselect'妸

', 'e

'union

allselect'發

', 'f

'union

allselect'旮

', 'g

'union

allselect'鉿

', 'h

'union

allselect'丌

', 'j

'union

allselect'咔

', 'k

'union

allselect'垃

', 'l

'union

allselect'嘸

', 'm

'union

allselect'拏

', 'n

'union

allselect'噢

', 'o

'union

allselect'妑

', 'p

'union

allselect'七

', 'q

'union

allselect'呥

', 'r

'union

allselect'仨

', 's

'union

allselect'他

', 't

'union

allselect'屲

', 'w

'union

allselect'夕

', 'x

'union

allselect'丫

', 'y

'union

allselect'帀

', 'z

'select

@strlen

=len

(@str

),@re='

'while

@strlen

>

0begin

select

top1

@re=

letter

+@re

,@strlen

=@strlen-1

from

@ta  

where

chr

<=

substring

(@str

,@strlen,1

) order

bychr  

desc

if@@rowcount=0

select

@re=

substring

(@str

,@strlen,1

)+@re,

@strlen

=@strlen-1

endreturn

(@re

) end

go--

>呼叫

select

*from

tb where

dbo.f_getpy(name)='

l'

獲取中文拼音首字母的SQL函式

create function dbo f getpy str nvarchar 4000 returns nvarchar 4000 asbegin declare word nchar 1 py nvarchar 4000 set py while len str 0 begin set wor...

SQL從中文中獲取拼音首字母

全部扔進去執行,不用修改 create function dbo.fnpbgetpyfirstletter str nvarchar 4000 select dbo.fnpbgetpyfirstletter 中國香港 returns nvarchar 4000 with encryption asb...

獲取漢字拼音首字母

有個專案需要用到生成漢字的首字母,但從網上查了一下,對於多音字的處理都不是很好,所以就利用pinyin4j這個工具包自己寫了乙個。用到的jar包是pinyin4j 2.5.0.jar 獲取拼音首字母,多音字用逗號隔開 public static string getfirstspell string...