生成文字首字母拼音码标量函数

--生成拼音首码
CREATE FUNCTION [dbo].[fn_GetPy] (@str nvarchar(4000)) returns nvarchar (4000) --WITH ENCRYPTION
AS
BEGIN
    DECLARE
        @intLen INT DECLARE
            @strRet nvarchar (4000) DECLARE
                @temp nvarchar (100)
            SET @intLen = len(@str)
            SET @strRet = ''
            WHILE @intLen > 0
            BEGIN
 
            SET @temp = '' SELECT
                @temp = CASE
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'Z'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'Y'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'X'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'W'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'T'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'S'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'R'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'Q'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'P'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'O'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'N'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'M'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'L'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'K'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'J'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'H'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'G'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'F'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'E'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'D'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'C'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'B'
            WHEN SUBSTRING (@str ,@intLen, 1) >= '' THEN
                'A'
            ELSE
                rtrim(
                    ltrim(SUBSTRING(@str ,@intLen, 1))
                )
            END --对于汉字特殊字符,不生成拼音码
            IF (ascii(@temp) > 127)
            SET @temp = '' --对于英文中小括号,不生成拼音码
            IF @temp = '('
            OR @temp = ')'
            SET @temp = '' SELECT
                @strRet = @temp + @strRet
            SET @intLen = @intLen - 1
            END RETURN LOWER (@strRet)
end

 

posted @ 2023-06-29 10:07  RC城  阅读(20)  评论(0编辑  收藏  举报