How to convert any Chinese character to the first letter of the Chinese character pinyin in SQL Server

Source: Internet
Author: User
Tags control characters

In fact, I have been writing this article for a long time, but it is only a variety of restrictions, so that I cannot take what I have learned to think is beneficial to everyone.
Shared.
The technology I want to talk about is useful to everyone. I used to develop a set of controls when I was building a large system.
One is the control that can be used to fuzzy query the Supplier name, that is, the operator does not have to select or enter code or
Write the name of the supplier to all people, a bit like Kingsoft. Later, some colleagues developed other systems and they used
I think it is good to enter the first letter of Chinese Character and pinyin, but later I learned that the input pinyin was used to create data.
I am inspired to add a column to store the pinyin. How can I convert any Chinese character into the first letter of the Chinese pinyin? I downloaded a Delphi program code on the Internet, which contains a vast majority of Chinese characters corresponding to pinyin. I processed the data.
After inserting the database and doing some processing, I wrote a database function for conversion.
The function is implemented in this way. I pasted this function code, but tens of thousands of data records are in the pinyin table of the database.
I will designate an address for you to download.
I have tested the efficiency problem: 80 thousand M memory, G saiyang, it takes about 3 seconds to convert Chinese characters. Of course, this function
It is mainly used for tables with hundreds of thousands of data records, which is mainly convenient for operators.
Create Function DBO. spellcode (
@ Sttext as varchar (1000)
)
Returns varchar (1000)
Begin

Declare
@ Returnvalue as varchar (1000 ),
@ Highvalue as int,
@ Lowvalue as int,
@ Apy as varchar (10)

Set @ returnvalue =''
While (LEN (@ sttext)> 0)
Begin
If (left (@ sttext, 1)> = 'A' and left (@ sttext, 1) <= 'Z') or (left (@ sttext, 1)> = 'A' and left (@ sttext, 1) <= 'Z') or (left (@ sttext, 1)> = '0' and left (@ sttext, 1) <= '9 ')
Begin
Set @ returnvalue = @ returnvalue + upper (left (@ sttext, 1 ))
Set @ sttext = right (@ sttext, Len (@ sttext)-1)
Continue
End

Set @ lowvalue = ASCII (left (@ sttext, 1 ))
If @ lowvalue <= 128 -- control characters and special characters are not processed
Begin
-- Set @ returnvalue = @ returnvalue + upper (left (@ sttext, 1) can be displayed if needed
Set @ sttext = right (@ sttext, Len (@ sttext)-1)
Continue
End
Set @ highvalue = cast (left (@ sttext, 1) as varbinary) as INT)-@ lowvalue * 256
If @ lowvalue = 166 -- Greek letter
Begin
Set @ apy =
Case when @ highvalue> = 161 and @ highvalue <= 184 then (select left (infovalue, 1) from chinesespells where rowid = @ highvalue-160 and typeinfo = 3)
When @ highvalue> = 193 and @ highvalue <= 216 then (select left (infovalue, 1) from chinesespells where rowid = @ highvalue-192 and typeinfo = 3)
End
Set @ returnvalue = @ returnvalue + upper (@ APY)
Set @ sttext = right (@ sttext, Len (@ sttext)-1)
Continue
End
-- Obtain the pinyin Index
Select @ apy = left (infovalue, 1) from chinesespell with (nolock) Where rowid = @ lowvalue-128 and subid = @ highvalue-63
If @ apy is null set @ apy = DBO. chtoen (left (@ sttext, 1 ))
Set @ returnvalue = @ returnvalue + upper (@ APY)

Set @ sttext = right (@ sttext, Len (@ sttext)-1)
End

Return @ returnvalue
End

 

 

 

 

 

 

 

 

 

 

 

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.