欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Microsoft SQL Server實(shí)現(xiàn)編寫漢字轉(zhuǎn)拼音函數(shù)

 更新時(shí)間:2024年03月12日 09:17:58   作者:初九之潛龍勿用  
在搜索應(yīng)用中,我們一般會提供一個(gè)搜索框,輸入關(guān)健字,點(diǎn)擊查詢按鈕以獲取結(jié)果數(shù)據(jù),大部分情況我們會提供模糊查詢的形式以在一個(gè)或多個(gè)字段進(jìn)行搜索以獲取結(jié)果,本文給大家就介紹了基于Microsoft SQL Server實(shí)現(xiàn)編寫漢字轉(zhuǎn)拼音函數(shù),需要的朋友可以參考下

應(yīng)用場景

在搜索應(yīng)用中,我們一般會提供一個(gè)搜索框,輸入關(guān)健字,點(diǎn)擊查詢按鈕以獲取結(jié)果數(shù)據(jù)。大部分情況我們會提供模糊查詢的形式以在一個(gè)或多個(gè)字段進(jìn)行搜索以獲取結(jié)果。這樣可以簡化用戶的操作,擴(kuò)大搜索范圍,為提高精度而提供基礎(chǔ)范圍數(shù)據(jù)。因此按漢字拼音搜索,即可以進(jìn)一步簡化輸入,又可以進(jìn)一步擴(kuò)大搜索范圍。

舉例

假設(shè)有字典表,表名 sys_d,包括 ID 和 NAME 字段,我們要對 NAME 字段進(jìn)行搜索,如下圖:

對于模糊搜索,我們可以通過 like 來實(shí)現(xiàn),比如我們想得到name字段中包含“職稱”的記錄,如下圖執(zhí)行:

用拼音簡碼的形式,可以更加進(jìn)一步的增加搜索范圍,并可以簡化切換輸入法的操作,比如輸入 ZC,即可以找到字典表中的數(shù)據(jù)。 因此我們可以編寫漢字轉(zhuǎn)拼音的函數(shù) GetPY 進(jìn)行進(jìn)一步操作,如下圖:

在搜索時(shí),我們通過該函數(shù)進(jìn)行了一次轉(zhuǎn)化,以得到預(yù)期結(jié)果,另外通過在查詢字段列表里進(jìn)行轉(zhuǎn)化驗(yàn)證,可以看到 PY 字段對應(yīng) NAME 的拼音簡寫轉(zhuǎn)化。

函數(shù)實(shí)現(xiàn)

打開SQL SERVER 查詢分析器,執(zhí)行如下代碼:

 
 
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è)字開始取
   set @length=len(@str)--輸入漢字的長度
   set @str1=''--用于存放返回值
   while @cyc<=@length
       begin  
          select @charcate=cast(substring(@str,@cyc,1) as varbinary)--每次取出一個(gè)字并將其轉(zhuǎn)變成二進(jìn)制,便于與GBK編碼表進(jìn)行比較
 if @charcate>=0XB0A1 and @charcate<=0XB0C4
         set @str1=@str1+'A'--說明此漢字的首字母為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+'J'
 else if @charcate>=0XBFA6 and @charcate<=0XC0AB
       set @str1=@str1+'K'
 else if @charcate>=0XC0AC and @charcate<=0XC2E7
       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'
 else 
	   set @str1 =@str1 + substring(@str,@cyc,1)
	   set @str1= ltrim(rtrim(@str1	))
       set @cyc=@cyc+1--取出輸入漢字的下一個(gè)字
 end
 return @str1--返回輸入漢字的首字母
 
       end
 
 
 
GO
 
 

GetPY函數(shù)需要傳遞 類型為varchar(500) 的字符串參數(shù)。

小結(jié)

以上代碼基于 Microsoft SQL SERVER 2016 編寫與實(shí)現(xiàn)。 實(shí)際的應(yīng)用中,還要結(jié)合原始輸入進(jìn)行查詢,可以使用或條件,拼音碼做為輔助查詢條件。另外,對于大數(shù)據(jù)量的表,可以采用空間換時(shí)間的做法,增加字段,存儲拼音簡寫值??梢酝ㄟ^在業(yè)務(wù)程序時(shí),錄入或修改功能實(shí)現(xiàn),也可以通過觸發(fā)器來實(shí)現(xiàn)。

以上就是基于Microsoft SQL Server實(shí)現(xiàn)編寫漢字轉(zhuǎn)拼音函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于SQL Server漢字轉(zhuǎn)拼音的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論