sql server如何獲取中文首字母函數(shù)
sql server獲取中文首字母函數(shù)
java的同學(xué)可以了解下pinyin4j,js可以了解ChinesePY.js
獲取拼音的意義多在于需對(duì)中文進(jìn)行排序,例如:通訊錄。
獲取漢字首字母的解決方案有
- 利用CJK統(tǒng)一表意符
- 利用GBK
- 利用Unicode編碼
利用Unicode編碼
第二個(gè)方法不太好用有的字獲取不出來(lái),這個(gè)比較頂
ALTER function [dbo].[GetPY](@str varchar(500)) returns varchar(500) as begin declare @word nchar(1),@PY nvarchar(4000) set @PY='' 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'錯(cuò)' 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
利用GBK
把@length=1改成@length=1len(@str) ,效果從輸入首字的首字母變成輸出全部字的首字母
create function [dbo].[GetPY](@str varchar(500))
returns varchar(500)
as
begin
declare @cyc int,@length int,@str1 varchar(100),@charcate varbinary(20)
set @cyc=1--從第幾個(gè)字開(kāi)始取
set @length=1--len(@str)--輸入漢字的長(zhǎng)度 只需要返回首字母
set @str1=''--用于存放返回值
while @cyc<=@length
begin
select @charcate=cast(substring(@str,@cyc,1) as varbinary)--每次取出一個(gè)字并將其轉(zhuǎn)變成二進(jìn)制,便于與GBK編碼表進(jìn)行比較
if @str LIKE '[a-z]%' --英文開(kāi)頭直接返回
return @str
if @charcate>=0XB0A1 and @charcate<=0XB0C4
set @str1=@str1+'a'--說(shuō)明此漢字的首字母為A,以下同上
else if @charcate>=0XB0C5 and @charcate<=0XB2C0
set @str1=@str1+'b'
else if @charcate>=0XB2C1 and @charcate<=0XB4ED
set @str1=@str1+'c'
else if @charcate>=0XB4EE and @charcate<=0XB6E9
set @str1=@str1+'d'
else if @charcate>=0XB6EA and @charcate<=0XB7A1
set @str1=@str1+'e'
else if @charcate>=0XB7A2 and @charcate<=0XB8C0
set @str1=@str1+'f'
else if @charcate>=0XB8C1 and @charcate<=0XB9FD
set @str1=@str1+'g'
else if @charcate>=0XB9FE and @charcate<=0XBBF6
set @str1=@str1+'h'
else if @charcate>=0XBBF7 and @charcate<=0XBFA5
set @str1=@str1+'i'
else if @charcate>=0XBFA6 and @charcate<=0XC0AB
set @str1=@str1+'j'
else if @charcate>=0XC0AC and @charcate<=0XC2E7
set @str1=@str1+'k'
else if @charcate>=0XC2E8 and @charcate<=0XC4C2
set @str1=@str1+'l'
else if @charcate>=0XC2E8 and @charcate<=0XC4C2
set @str1=@str1+'m'
else if @charcate>=0XC4C3 and @charcate<=0XC5B5
set @str1=@str1+'n'
else if @charcate>=0XC5B6 and @charcate<=0XC5BD
set @str1=@str1+'o'
else if @charcate>=0XC5BE and @charcate<=0XC6D9
set @str1=@str1+'p'
else if @charcate>=0XC6DA and @charcate<=0XC8BA
set @str1=@str1+'q'
else if @charcate>=0XC8BB and @charcate<=0XC8F5
set @str1=@str1+'r'
else if @charcate>=0XC8F6 and @charcate<=0XCBF9
set @str1=@str1+'s'
else if @charcate>=0XCBFA and @charcate<=0XCDD9
set @str1=@str1+'t'
else if @charcate>=0XCDDA and @charcate<=0XCEF3
set @str1=@str1+'w'
else if @charcate>=0XCEF4 and @charcate<=0XD1B8
set @str1=@str1+'x'
else if @charcate>=0XD1B9 and @charcate<=0XD4D0
set @str1=@str1+'y'
else if @charcate>=0XD4D1 and @charcate<=0XD7F9
set @str1=@str1+'z'
set @cyc=@cyc+1--取出輸入漢字的下一個(gè)字
end
return @str1--返回輸入漢字的首字母
end總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SqlServer2012中LEAD函數(shù)簡(jiǎn)單分析
SQL SERVER 2012 T-SQL新增幾個(gè)聚合函數(shù): FIRST_VALUE LAST_VALUE LEAD LAG,今天我們首先來(lái)簡(jiǎn)單分析下LEAD,希望對(duì)大家有所幫助,能夠盡快熟悉這個(gè)聚合函數(shù)2014-08-08
SQL Server中的RAND函數(shù)的介紹和區(qū)間隨機(jī)數(shù)值函數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了SQL Server中的RAND函數(shù)的介紹和區(qū)間隨機(jī)數(shù)值函數(shù)的實(shí)現(xiàn) 的相關(guān)資料,需要的朋友可以參考下2015-12-12
使用sqlserver存儲(chǔ)過(guò)程sp_send_dbmail發(fā)送郵件配置方法(圖文)
這篇文章用圖文的方式介紹了使用sqlserver存儲(chǔ)過(guò)程sp_send_dbmail發(fā)送郵件的方法,大家參考使用吧2014-01-01
SQL Server修改數(shù)據(jù)庫(kù)名稱(chēng)的常用方法
本文詳細(xì)介紹如何在SOLServer中更改數(shù)據(jù)庫(kù)名稱(chēng),包括通過(guò)圖形界面、ALTER DATABASE命令及使用SP RENAMEDB系統(tǒng)存儲(chǔ)過(guò)程的方法,并提供確保操作成功的前置步驟如殺死會(huì)話或設(shè)置單用戶(hù)模式,需要的朋友可以參考下2025-10-10
簡(jiǎn)化SQL Server備份與還原到云工作原理及操作方法
您可以使用 SQL Server 的本機(jī)備份功能來(lái)備份您的 SQL Server Database到 Windows AzureBlob 存儲(chǔ)服務(wù),也可以使用 T-SQL 和SMO備份到Windows AzureBlob存儲(chǔ),感興趣的可以了解下本文,或許可以幫助到你2013-02-02
SqlServer 數(shù)據(jù)庫(kù) 三大 范式
本文將基于三大范式原則,結(jié)合具體的實(shí)例做簡(jiǎn)要分析,對(duì)SqlServer 數(shù)據(jù)庫(kù) 三大 范式相關(guān)知識(shí)感興趣的朋友一起看看吧2019-11-11

