Microsoft SQL Server 编写汉字转拼音函数

应用场景

在搜索应用中,我们一般会提供一个搜索框,输入关健字,点击查询按钮以获取结果数据。大部分情况我们会提供模糊查询的形式以在一个或多个字段进行搜索以获取结果。这样可以简化用户的操作,扩大搜索范围,为提高精度而提供基础范围数据。因此按汉字拼音搜索,即可以进一步简化输入,又可以进一步扩大搜索范围。

函数实现

打开SQL SERVER 查询分析器,执行如下代码:

csharp 复制代码
 
 
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--从第几个字开始取
   set @length=len(@str)--输入汉字的长度
   set @str1=''--用于存放返回值
   while @cyc<=@length
       begin  
          select @charcate=cast(substring(@str,@cyc,1) as varbinary)--每次取出一个字并将其转变成二进制,便于与GBK编码表进行比较
 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--取出输入汉字的下一个字
 end
 return @str1--返回输入汉字的首字母
 
       end
 
 
 
GO
 
 

调用

csharp 复制代码
 SELECT TOP 10000 xf_madein,[Desc],dbo.GetPY(xf_madein) AS DDD,dbo.GetPY([Desc]) AS DDD222 FROM dbo.CNL_Label
相关推荐
市象10 小时前
傅利叶十周年,升级核心战略:“有温度”的具身智能蓝图
microsoft
qq_393828221 天前
Windows ABBYY FineReader 16 Corporate 文档转换、PDF编辑和文档比较
windows·microsoft·电脑·开源软件·软件需求
高工智能汽车1 天前
AI汽车时代的全面赋能者:德赛西威全栈能力再升级
人工智能·microsoft·汽车
带娃的IT创业者1 天前
《AI大模型应知应会100篇》第58篇:Semantic Kernel:微软的大模型应用框架
人工智能·microsoft·flask
Leinwin1 天前
Microsoft Azure 在印度尼西亚区域正式上线
microsoft·azure
冰茶_2 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式
vortex52 天前
微软输入法常用快捷键介绍以及调教技巧
microsoft
水煮蛋不加蛋2 天前
AutoGen 框架解析:微软开源的多人 Agent 协作新范式
人工智能·microsoft·ai·开源·大模型·llm·agent
Yungoal2 天前
IIS配置SSL
网络协议·microsoft·ssl
CoderJia程序员甲2 天前
AI 入门资源:微软 AI-For-Beginners 项目指南
人工智能·microsoft·ai·ai编程