The MSSQL function of obtaining the first letter of phonetic Alphabet of Chinese character string

Source: Internet
Author: User
Tags mssql return
function | Chinese character | pinyin | string drop function fun_getpy
Go
SET QUOTED_IDENTIFIER ON
Go
SET ANSI_NULLS on
Go
----A stored procedure to take the first letter of Chinese pinyin
Create function fun_getpy (@str nvarchar (4000))

Returns nvarchar (4000)
As
Begin

DECLARE @word nchar (1), @PY nvarchar (4000)

Set @PY = '

While Len (@str) >0
Begin
Set @word =left (@str, 1)

--If non-Chinese characters, return the original character
Set @PY = @PY + (case when Unicode (@word) between 19968 and 19968+20901
Then (
Select top 1 PY
From
(
Select ' A ' as Py,n ' 驁 ' as Word
UNION ALL SELECT ' B ', N ' book '
UNION ALL SELECT ' C ', N ' wrong '
UNION ALL SELECT ' D ', N ' 鵽 '
UNION ALL SELECT ' E ', N ' 樲 '
UNION ALL SELECT ' F ', N ' 鰒 '
UNION ALL SELECT ' G ', N ' hiker '
UNION ALL SELECT ' H ', N ' 夻 '
UNION ALL SELECT ' J ', N ' 攈 '
UNION ALL SELECT ' K ', N ' 穒 '
UNION ALL SELECT ' L ', N ' 鱳 '
UNION ALL SELECT ' M ', N ' temperature '
UNION ALL SELECT ' n ', n ' 桛 '
UNION ALL SELECT ' O ', N ' 漚 '
UNION ALL SELECT ' P ', N ' exposure '
UNION ALL SELECT ' Q ', N ' 囕 '
UNION ALL SELECT ' R ', N ' 鶸 '
UNION ALL SELECT ' S ', N ' 蜶 '
UNION ALL SELECT ' T ', N ' 籜 '
UNION ALL SELECT ' W ', N ' 鶩 '
UNION ALL SELECT ' X ', N ' 鑂 '
UNION ALL SELECT ' Y ', N ' rhythmic '
UNION ALL SELECT ' Z ', N ' out '
) T
where word>= @word collate chinese_prc_cs_as_ks_ws
ORDER BY PY ASC
)
else @word
End
Set @str =right (@str, Len (@str)-1)
End

Return @PY

End
Go
IF object_id (' dbo.fun_getpy ') is not NULL
PRINT ' <<< CREATED FUNCTION dbo.fun_getpy >>> '
ELSE
PRINT ' <<< FAILED creating FUNCTION dbo.fun_getpy >>> '
Go
SET ansi_nulls off
Go
SET QUOTED_IDENTIFIER OFF
Go

Call
Select Dbo.fun_getpy (' Dark blue ')

Back: SL


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.