SQL标量值函数:返回汉字拼音首拼

今天遇到一个需求,客户要求在系统客户端选择客户的时候,可以用拼音首拼去快速过滤选择,此时我们在客户表里面加多一个拼音首拼字段CustPY来记录,字段加好了,我们要把所有客户名称的拼音简拼都更新到CustPY字段中。

为此利用以下函数做了个批量更新

执行语句:UPDATE  客户表 SET  CustPY=dbo.[Fn_GetPinYin](客户名称字段)

这样就可以了。需要注意的是:多音字。例如 SELECT dbo.[Fn_GetPinYin]('重庆') 返回ZQ    谨此记录。

-- =============================================
-- 调用:SELECT dbo.[Fn_GetPinYin]('中国')
-- Create date: 2015-01-06
-- Description:    返回汉字拼音首拼
-- =============================================
CREATE  FUNCTION [dbo].[Fn_GetPinYin] (@str NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
    BEGIN
        DECLARE @word NCHAR(1) ,
            @PY NVARCHAR(4000)

        SET @PY=''

        SET @str=REPLACE(@str, '(', '(')
        SET @str=REPLACE(@str, ')', ')')

        WHILE LEN(@str)>0
            BEGIN
                SET @word=LEFT(@str, 1)

                --如果非汉字字符,返回原字符
                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'簿'
                                                                                                     UNION ALL
                                                                                                     SELECT 'C' ,
                                                                                                            N'錯'
                                                                                                     UNION ALL
                                                                                                     SELECT 'D' ,
                                                                                                            N'鵽'
                                                                                                     UNION ALL
                                                                                                     SELECT 'E' ,
                                                                                                            N'樲'
                                                                                                     UNION ALL
                                                                                                     SELECT 'F' ,
                                                                                                            N'鰒'
                                                                                                     UNION ALL
                                                                                                     SELECT 'G' ,
                                                                                                            N'腂'
                                                                                                     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'旀'
                                                                                                     UNION ALL
                                                                                                     SELECT 'N' ,
                                                                                                            N'桛'
                                                                                                     UNION ALL
                                                                                                     SELECT 'O' ,
                                                                                                            N'漚'
                                                                                                     UNION ALL
                                                                                                     SELECT 'P' ,
                                                                                                            N'曝'
                                                                                                     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'韻'
                                                                                                     UNION ALL
                                                                                                     SELECT 'Z' ,
                                                                                                            N'咗'
                                                                                                    ) 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

 

如果朋友们觉得以后可能会用到的话,欢迎转载,当然也可以推荐一下,谢谢。

PS:此博文是利用Windows Live Writer 2012编写,格式效果可能不太好。

转载于:https://www.cnblogs.com/lhmlyx2723356/p/4232920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值