SQL Server field extract Pinyin initials

Source: Internet
Author: User

At present, a situation encountered in the work, you need to extract a field in SQL Server Pinyin first letter, the field by the Chinese characters, English, digital and "-" composition, Baidu a bunch, find the following method, record, for later use!

First set up a function

--generate pinyin first code  CREATE  functionFn_getpy (@str  nvarchar(4000))    returns  nvarchar(4000)    --With encryption   as    begin    Declare  @intLen int    Declare  @strRet nvarchar(4000)    Declare  @temp  nvarchar( -)    Set  @intLen  =  Len(@str)    Set  @strRet  =  "'     while  @intLen  >  0    begin    Set  @temp  =  "'    Select  @temp  =   Case       when  substring(@str,@intLen,1)>=  'as'   Then  'Z'     when  substring(@str,@intLen,1)>=  'ya'   Then  'Y'     when  substring(@str,@intLen,1)>=  'XI'   Then  'X'     when  substring(@str,@intLen,1)>=  '屲'   Then  'W'     when  substring(@str,@intLen,1)>=  'his'   Then  'T'     when  substring(@str,@intLen,1)>=  'SA'   Then  'S'     when  substring(@str,@intLen,1)>=  '呥'   Then  'R'     when  substring(@str,@intLen,1)>=  'Seven'   Then  'Q'     when  substring(@str,@intLen,1)>=  '妑'   Then  'P'     when  substring(@str,@intLen,1)>=  'Oh'   Then  'O'     when  substring(@str,@intLen,1)>=  'Hallasan'   Then  'N'     when  substring(@str,@intLen,1)>=  '嘸'   Then  'M'     when  substring(@str,@intLen,1)>=  ' Garbage'   Then  'L'     when  substring(@str,@intLen,1)>=  'ka'   Then  'K'     when  substring(@str,@intLen,1)>=  ' not'   Then    'J'     when  substring(@str,@intLen,1)>=  'hafnium'   Then  'H'     when  substring(@str,@intLen,1)>=  'Xu'   Then  'G'     when  substring(@str,@intLen,1)>=  'Hair'   Then  'F'     when  substring(@str,@intLen,1)>=  'Ehegan'   Then  'E'     when  substring(@str,@intLen,1)>=  'Otah'   Then  'D'     when  substring(@str,@intLen,1)>=  'Cha'   Then  'C'     when  substring(@str,@intLen,1)>=  'Eight'   Then  'B'     when  substring(@str,@intLen,1)>=  'Acridine'   Then  'A'    Else  RTrim(LTrim(substring(@str,@intLen,1)))    End    --for kanji special characters, no phonetic codes are generated  if(ASCII(@temp)>127)Set  @temp  =  "'    --for English parentheses, no phonetic codes are generated  if  @temp  =  '('  or  @temp  =  ')'  Set  @temp  =  "'    Select  @strRet  =  @temp  +  @strRet    Set  @intLen  =  @intLen  -  1    End    return  Lower(@strRet)    End 

EXECUTE statement

SELECT     as Column Aliases  from        Table name

Thank you: Luckeryin

SQL Server field extract Pinyin initials

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.