create function 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 '吖','A' union all select '八','B' union all
  select '嚓','C' union all select '咑','D' union all
  select '妸','E' union all select '发','F' union all
  select '旮','G' union all select '铪','H' union all
  select '丌','J' union all select '咔','K' union all
  select '垃','L' union all select '嘸','M' union all
  select '拏','N' union all select '噢','O' union all
  select '妑','P' union all select '七','Q' union all
  select '呥','R' union all select '仨','S' union all
  select '他','T' union all select '屲','W' union all
  select '夕','X' union all select '丫','Y' union all
  select '帀','Z'
  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
go--调用
select * from tb where dbo.f_GetPy(Name) like 'z%'

解决方案 »

  1.   

    CREATE function fn_GetPy(@str nvarchar(4000))
    returns nvarchar(4000)
    -- WITH ENCRYPTION
    as
    begin
    declare @intLen int
    declare @strRet nvarchar(4000)
    declare @temp nvarchar(100) set @intLen = len(@str)
    set @strRet = '' while @intLen > 0
    begin
    set @temp = '' select @temp = case 
    when substring(@str,@intLen,1) >= '帀' then 'Z'
    when substring(@str,@intLen,1) >= '丫' then 'Y'
    when substring(@str,@intLen,1) >= '夕' then 'X'
    when substring(@str,@intLen,1) >= '屲' then 'W'
    when substring(@str,@intLen,1) >= '他' then 'T'
    when substring(@str,@intLen,1) >= '仨' then 'S'
    when substring(@str,@intLen,1) >= '呥' then 'R'
    when substring(@str,@intLen,1) >= '七' then 'Q'
    when substring(@str,@intLen,1) >= '妑' then 'P'
    when substring(@str,@intLen,1) >= '噢' then 'O'
    when substring(@str,@intLen,1) >= '拏' then 'N'
    when substring(@str,@intLen,1) >= '嘸' then 'M'
    when substring(@str,@intLen,1) >= '垃' then 'L'
    when substring(@str,@intLen,1) >= '咔' then 'K'
    when substring(@str,@intLen,1) >= '丌' then  'J'
    when substring(@str,@intLen,1) >= '铪' then 'H'
    when substring(@str,@intLen,1) >= '旮' then 'G'
    when substring(@str,@intLen,1) >= '发' then 'F'
    when substring(@str,@intLen,1) >= '妸' then 'E'
    when substring(@str,@intLen,1) >= '咑' then 'D'
    when substring(@str,@intLen,1) >= '嚓' then 'C'
    when substring(@str,@intLen,1) >= '八' then 'B'
    when substring(@str,@intLen,1) >= '吖' then 'A'
    else rtrim(ltrim(substring(@str,@intLen,1)))
    end --对于汉字特殊字符,不生成拼音码
    if (ascii(@temp)>127) set @temp = '' --对于英文中小括号,不生成拼音码
    if @temp = '(' or @temp = ')' set @temp = '' select @strRet = @temp + @strRet set @intLen = @intLen - 1
    end return lower(@strRet)
    --调用 
    select * from  tb where dbo.fn_GetPy(name) like 'z%'
      

  2.   

    么产生GB2312汉字列表 select top 71 id=identity(int,176,1) into #a from syscolumns
    select top 94 id=identity(int,161,1) into #b from syscolumns
    select ch=cast(cast(a.id as binary(1))+cast(b.id as binary(1)) as char(2)) 
    collate chinese_prc_cs_as_ks_ws
    from #a a,#b b
    where not(a.id=215 and b.id between 250 and 254) 
    order by ch是什么意思啊??