SQL automatically generates pinyin code functions

Source: Internet
Author: User

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_ch2py] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_ch2py]
Go

Set quoted_identifier off
Go
Set ansi_nulls off
Go

-- Create the pinyin function-basic function 1 to be called
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

-- Usage
/*
Select DBO. f_ch2py ('中') -- Z
Select DBO. f_ch2py ('country ') -- g
Select DBO. f_ch2py ('people') -- r
Select DBO. f_ch2py ('hour') -- m
*/

Go
Set quoted_identifier off
Go
Set ansi_nulls on
Go

------------------------------------------------------

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_gethelpcode] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_gethelpcode]
Go

Set quoted_identifier off
Go
Set ansi_nulls off
Go

-- Create the pinyin function-basic function 2 to be called
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

-- Usage
/*
Select DBO. f_gethelpcode ('power') 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
Set quoted_identifier off
Go
Set ansi_nulls on
Go

------------------------------------------------------

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

Set quoted_identifier off
Go
Set ansi_nulls off
Go

-- Create a pinyin Function
Create Function fgetpy (@ STR varchar (500) = '')
Returns varchar (500)
As
Begin
Declare @ strlen int, @ return varchar (500), @ 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
Select @ II = @ II + 1, @ n = 63, @ CHN = substring (@ STR, @ II, 1)
If @ CHN> 'Z'
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 'haid'
Union all select '-- because have no' I'
Union all select 'stop'
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 'hour' -- no 'U'
Union all select 'hour' -- no 'V'
Union all select '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
Else set @ C = @ CHN
Set @ return = @ return + @ C
End
Return (@ return)
End

-- Usage
/*
-- Test
Select DBO. fgetpy ('dongguan City ') as Dongguan City, DBO. fgetpy (' AB Chinese C') as AB Chinese people
*/

 

Go
Set quoted_identifier off
Go
Set ansi_nulls on
Go

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.