Sql Server 汉字转拼音首写字母(无数据表版)

/* -------------------------------------------------------------
函数: [CreateFirstLetter]
描述: 汉字转拼音首写字母(无数据表版)
使用: dbo.[CreateFirstLetter]('中华人民共和国') = ZHRMGHG
改编:Tony
------------------------------------------------------------- */

IF OBJECT_ID('[CreateFirstLetter]') IS NOT NULL
DROP FUNCTION [CreateFirstLetter]
GO

Create function [dbo].[CreateFirstLetter]
(
    @col varchar(2000)
)
returns varchar(2000)
    begin
        declare @cyc int,@len int,@sql varchar(1000),@char varbinary(20)
        select @cyc = 1,@len = len(@col),@sql = ''
        while @cyc <= @len 
            begin  
                select @char = cast(substring(@col, @cyc, 1) as varbinary)
                if @char>=0XB0A1 and @char<=0XB0C4      set @sql=@sql+'A'
                else if @char>=0XB0C5 and @char<=0XB2C0 set @sql=@sql+'B'
                else if @char>=0XB2C1 and @char<=0XB4ED set @sql=@sql+'C'
                else if @char>=0XB4EE and @char<=0XB6E9 set @sql=@sql+'D'
                else if @char>=0XB6EA and @char<=0XB7A1 set @sql=@sql+'E'
                else if @char>=0XB7A2 and @char<=0XB8C0 set @sql=@sql+'F'
                else if @char>=0XB8C1 and @char<=0XB9FD set @sql=@sql+'G'
                else if @char>=0XB9FE and @char<=0XBBF6 set @sql=@sql+'H'
                else if @char>=0XBBF7 and @char<=0XBFA5 set @sql=@sql+'J'
                else if @char>=0XBFA6 and @char<=0XC0AB set @sql=@sql+'K'
                else if @char>=0XC0AC and @char<=0XC2E7 set @sql=@sql+'L'
                else if @char>=0XC2E8 and @char<=0XC4C2 set @sql=@sql+'M'
                else if @char>=0XC4C3 and @char<=0XC5B5 set @sql=@sql+'N'
                else if @char>=0XC5B6 and @char<=0XC5BD set @sql=@sql+'O'
                else if @char>=0XC5BE and @char<=0XC6D9 set @sql=@sql+'P'
                else if @char>=0XC6DA and @char<=0XC8BA set @sql=@sql+'Q'
                else if @char>=0XC8BB and @char<=0XC8F5 set @sql=@sql+'R'
                else if @char>=0XC8F6 and @char<=0XCBF9 set @sql=@sql+'S'
                else if @char>=0XCBFA and @char<=0XCDD9 set @sql=@sql+'T'
                else if @char>=0XCDDA and @char<=0XCEF3 set @sql=@sql+'W'
                else if @char>=0XCEF4 and @char<=0XD1B8 set @sql=@sql+'X'
                else if @char>=0XD1B9 and @char<=0XD4D0 set @sql=@sql+'Y'
                else if @char>=0XD4D1 and @char<=0XD7F9 set @sql=@sql+'Z'
                set @cyc = @cyc + 1 
            end
        return @sql
    end
    GO

posted @ 2013-02-27 11:41  TNZYM  阅读(230)  评论(0编辑  收藏  举报