How to obtain the first letter of a group of Chinese characters (SQL Server)

Source: Internet
Author: User
-- Supports 20000 Chinese characters in a large character set!

Create Function f_ch2py (@ CHN nchar (1 ))
Returns char (1)
As
Begin
Declare @ n int
Declare @ C char (1)
Set @ n = 63

Select @ n = @ n + 1,
@ C = case CHN when @ CHN then Char (@ n) else @ C end
From (
Select top 27 * from (
Select CHN =
'Ay' Union all select
'8' Union all select
'Signature' Union all select
'Signature' Union all select
'Signature' Union all select
'Output' Union all select
'Signature' Union all select
'Hagh' Union all select
'Open 'Union all select -- because have no 'I'
'Open 'Union all select
'Signature' Union all select
'Signature' Union all select
'Signature' Union all select
'Signature' Union all select
'Ou' Union all select
'Signature' Union all select
'7' Union all select
'Signature' Union all select
'Signature' Union all select
'Others' Union all select
'Ignore' Union all select -- no 'U'
'Hangzhou' Union all select -- no 'V'
'Signature' Union all select
'Xi 'Union all select
'Ya 'Union all select
'Signature' Union all select @ CHN) as
Order by CHN collate chinese_prc_ci_as
) As B
Return (@ C)
End
Go

Select DBO. f_ch2py ('中') -- Z
Select DBO. f_ch2py ('country ') -- g
Select DBO. f_ch2py ('people') -- r
Select DBO. f_ch2py ('hour') -- m
Go
----------------- Call
Create Function f_gethelpcode (
@ Cname varchar (20 ))
Returns varchar (12)
As
Begin
Declare @ I smallint, @ l smallint, @ chelpcode varchar (12), @ E varchar (12), @ iascii smallint
Select @ I = 1, @ l = 0, @ chelpcode =''
While @ l <= 12 and @ I <= Len (@ cname) begin
Select @ E = lower (substring (@ cname, @ I, 1 ))
Select @ iascii = ASCII (@ E)
If @ iascii> = 48 and @ iascii <= 57 or @ iascii> = 97 and @ iascii <= 122 or @ iascii = 95
Select @ chelpcode = @ chelpcode + @ E
Else
If @ iascii & gt; = 176 and @ iascii <= 247
Select @ chelpcode = @ chelpcode + DBO. f_ch2py (@ E)
Else select @ l = @ L-1
Select @ I = @ I + 1, @ l = @ L + 1 end
Return @ chelpcode
End
Go

-- Call
Select DBO. f_gethelpcode ('Big direction ')

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.