>  기사  >  데이터 베이스  >  한자를 병음의 첫 글자로 변환하는 매우 효율적인 기능

한자를 병음의 첫 글자로 변환하는 매우 효율적인 기능

大家讲道理
大家讲道理원래의
2016-11-10 11:22:021852검색

시나리오:

사용자가 입력한 이름 키워드로 사용자를 검색해야 합니다. 사용자는 사용자를 검색하기 위해 키워드 'x'를 입력합니다(데이터는 테이블 [이름 필드] 또는 메모리 [목록]에서 제공됨)

요구 사항:

결과 정렬은 다음과 같습니다. be :

  • x

  • xia

  • xiao

  • yx

즉,

x 문자가 포함된 결과가 표시되어야 합니다.

첫 번째 문자와 일치하는 결과가 먼저 표시되어야 합니다. (예: 시작 부분의 x)

동일한 조건 2에서는 짧은 결과가 먼저 순위가 지정되어야 합니다. (예: x가 xia보다 먼저 순위가 지정됩니다.)

create function [dbo].[fn_getpy2](@Str varchar(500)='')
returns varchar(500)
as
begin
 declare @strlen int,@return varchar(500),@ii int
 declare @n int,@c char(1),@chn nchar(1)

 select @strlen=len(@str),@return='',@ii=0
 set @ii=0
 while @ii<@strlen
 begin
  select @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)
  if @chn>&#39;z&#39;
  select @n = @n +1
     ,@c = case chn when @chn then char(@n) else @c end
   from(
    select top 27 * from (
     select chn = &#39;吖&#39;
     union all select &#39;八&#39;
     union all select &#39;嚓&#39;
     union all select &#39;咑&#39;
     union all select &#39;妸&#39;
     union all select &#39;发&#39;
     union all select &#39;旮&#39;
     union all select &#39;铪&#39;
     union all select &#39;丌&#39;  --because have no &#39;i&#39;
     union all select &#39;丌&#39;
     union all select &#39;咔&#39;
     union all select &#39;垃&#39;
     union all select &#39;嘸&#39;
     union all select &#39;拏&#39;
     union all select &#39;噢&#39;
     union all select &#39;妑&#39;
     union all select &#39;七&#39;
     union all select &#39;呥&#39;
     union all select &#39;仨&#39;
     union all select &#39;他&#39;
     union all select &#39;屲&#39;  --no &#39;u&#39;
     union all select &#39;屲&#39;  --no &#39;v&#39;
     union all select &#39;屲&#39;
     union all select &#39;夕&#39;
     union all select &#39;丫&#39;
     union all select &#39;帀&#39;
     union all select @chn) as a
    order by chn COLLATE Chinese_PRC_CI_AS 
   ) as b
  else set @c=@chn
  set @return=@return+@c
 end
 return(@return)
end


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.