SQL Server中获得汉字首个字母的函数

--取汉字拼音首字母的函数
ALTER   FUNCTION [dbo].[uf_GetHZPinYin](@str nvarchar(100))
  RETURNS nvarchar(100)
AS
BEGIN
  DECLARE
    @word nchar(1),
    @PY   nvarchar(100)
  SET @PY = ''
  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 left(@PY,5)--返回前面5个字符
END

 

posted on 2010-01-16 17:33  DoNet鸟  阅读(301)  评论(0编辑  收藏  举报

导航