SqlServer的汉字转拼音码的函数

代码
--
--
 Definition for user-defined function f_GetPy : 
--
GO
create   function   [dbo].f_GetPy(@str   nvarchar(4000)) 
returns   nvarchar(4000
as 
begin 
declare   @strlen   int,@re   nvarchar(4000
declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 
insert   into   @t(chr,letter) 
    
select   '吖 '''   union   all   select   '八 '''   union   all 
    
select   '嚓 '''   union   all   select   '咑 '''   union   all 
    
select   '妸 '''   union   all   select   '发 '''   union   all 
    
select   '旮 '''   union   all   select   '铪 '''   union   all 
    
select   '丌 '''   union   all   select   '咔 '''   union   all 
    
select   '垃 '''   union   all   select   '嘸 '''   union   all 
    
select   '拏 '''   union   all   select   '噢 '''   union   all 
    
select   '妑 '''   union   all   select   '七 '''   union   all 
    
select   '呥 '''   union   all   select   '仨 '''   union   all 
    
select   '他 '''   union   all   select   '屲 '''   union   all 
    
select   '夕 '''   union   all   select   '丫 '''   union   all 
    
select   '帀 ''' 
    
select   @strlen=len(@str),@re= ' ' 
    
while   @strlen> 0 
    
begin 
        
select   top   1   @re=letter+@re,@strlen=@strlen-1 
            
from   @t   a   where   chr <=substring(@str,@strlen,1
            
order   by   chr   desc 
        
if   @@rowcount=0 
            
select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 
    
end 
    
return(@re
end

代码
//用法一
string sql = string.Format("Name like '%{0}%' or dbo.f_GetPy(Name) like '{0}%' ", goodsName);

//用法二
string sql = string.Format(@"Select * from (Select Company_Name from tb_enterprise where Company_Name like '%{1}%' or F_PINYIN(Company_Name) like '%{1}%' ) 
                                         WHERE ROWNUM <= {0} ORDER BY ROWNUM ASC
", topCount, name);
原文地址:https://www.cnblogs.com/kokoliu/p/1657784.html