SQL標量值函數:返回漢字拼音首拼

今天遇到一個需求,客戶要求在系統客戶端選擇客戶的時候,能夠用拼音首拼去快速過濾選擇,此時咱們在客戶表裏面加多一個拼音首拼字段CustPY來記錄,字段加好了,咱們要把全部客戶名稱的拼音簡拼都更新到CustPY字段中。函數

爲此利用如下函數作了個批量更新ip

執行語句:UPDATE  客戶表 SET  CustPY=dbo.[Fn_GetPinYin](客戶名稱字段) it

這樣就能夠了。須要注意的是:多音字。例如 SELECT dbo.[Fn_GetPinYin]('重慶') 返回ZQ    謹此記錄。
io

-- =============================================
-- 調用: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) date

        SET @PY='' 客戶端

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

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

                --若是非漢字字符,返回原字符
                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 block

        RETURN @PY 字符

    END

 

若是朋友們以爲之後可能會用到的話,歡迎轉載,固然也能夠推薦一下,謝謝。

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

相關文章
相關標籤/搜索