This function is written in SQL SERVER 2008. Due to limited time, there are still many problems to be solved: If you need a database with pinyin words (some databases only have the first pinyin words, not Suitable). Currently, this function can only convert a string of Chinese characters (without any punctuation, letters or special characters in the middle. I will improve this function if I have time. Please give me more instructions.
Create function streetii_PinYin_Convert_Class
(
@ Word nvarchar (128)
)
Returns @ temp table
(
Id int identity (1, 1 ),
Word nvarchar (128 ),
Pinyin nvarchar (255)
)
As
Begin
Declare @ pinyin nvarchar (16)
Declare @ char nvarchar (4)
Declare @ currentindex int = 1
Declare @ totalpinyin nvarchar (256) =''
Declare @ length int = len (@ word) -- check the length of the string
If @ word is not null
Begin
While @ currentindex <= @ Length
Begin
Set @ char = substring (@ word, @ currentindex, 1) -- split characters one by one and assign values to the variable @ char
Set @ pinyin = (select pinyin from streetii_pinyinforword where WORD = @ char) -- get the Pinyin of characters in @ char
Set @ totalpinyin = @ totalpinyin + @ Pinyin -- add to variable
Set @ currentindex = @ currentindex + 1 -- detects the next Chinese Character
End
Insert into @ temp values (@ word, @ totalpinyin)
End
Return
End
/*
Select * From streetii_pinyin_convert_class ('Welcome to Beijing ')
*/