Get the first letter of a Chinese character string (T-SQL function)

Source: Internet
Author: User
Tags strlen
function | Chinese Character | string

/*--obtains the first letter of Chinese character string--*/
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ FGETPY] and Xtype in (n ' FN ', n ' IF ', n ' TF '))
Drop function [dbo]. [Fgetpy]
Go

--Create phonetic function
Create function fgetpy (@Str varchar (500) = ')
Returns varchar (500)
As
begin--function Implementation begins

declare @strlen int, @return varchar, @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--while Cycle Start
Select @ii = @ii +1,@n=63, @chn =substring (@str, @ii, 1)

If @chn > ' z '--if start
--There is only one SELECT statement, the principle of "string sorting and ASCII code table":
Select @n = @n +1,@c =--★★★select★★★
Case CHN--case Start
When @chn then char (@n)--case Branch
else @c--case Branch
End--case Ends
From (--★★★from★★★
Select top of * FROM (
Select CHN = ' Acridine '
UNION ALL select ' Eight '
UNION ALL select ' Cha '
UNION ALL SELECT ' 咑 '
UNION ALL SELECT ' 妸 '
UNION ALL SELECT ' Send '
UNION ALL SELECT ' There '
UNION ALL SELECT ' Hafnium '
UNION ALL SELECT ' No '--because have no ' i '
UNION ALL SELECT ' No '
UNION ALL select ' Ka '
UNION ALL select ' garbage '
UNION ALL SELECT ' 嘸 '
UNION ALL SELECT ' 拏 '
UNION ALL select ' Oh '
UNION ALL SELECT ' 妑 '
UNION ALL select ' Seven '
UNION ALL SELECT ' 呥 '
UNION ALL select ' sa '
UNION ALL SELECT ' he '
UNION ALL SELECT ' 屲 '--no ' u '
UNION ALL SELECT ' 屲 '--no ' V '
UNION ALL SELECT ' 屲 '
UNION ALL select ' XI '
UNION ALL select ' Ya '
UNION ALL SELECT ' 帀 '
UNION ALL Select @chn) as a
ORDER BY CHN COLLATE Chinese_prc_ci_as
) as B

else--if the corresponding else
Set @c= @chn
--if End

Set @return = @return +@c
End--while Loop End

Return (@return)
End of end--function implementation

Go
--Test
Select Dbo.fgetpy (' Wei Baoquan ') as name Pinyin, dbo.fgetpy (' C people in AB ') as Chinese
Select Dbo.fgetpy (' Liu Ziliang ') as name Pinyin, dbo.fgetpy (' C people in AB ') as Chinese
Select Dbo.fgetpy (' Wu-too ') as name Pinyin, Dbo.fgetpy (' C Chinese ') as China
Select Dbo.fgetpy (' northeast ') as name Pinyin, dbo.fgetpy (' C people in AB ') as Chinese
Select Dbo.fgetpy (' Wang Dahai VIII ') as Wang Dahai eight ya, dbo.fgetpy (' service map ') as service map

--Removing phonetic functions
Drop function Fgetpy



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.