啊,你要到Z呀,那ZZ是不是就是2626?

解决方案 »

  1.   

    create function f_convert(@str varchar(100))
    returns bigint
    as
    begin
        declare @i bigint
        declare @c varchar(2)
        set @i = 0
        while(len(@str)>0)
        begin
            set @c=UPPER(LEFT(@str,1))
            
            if(@c between 'A' and 'Z')
                set @i=@i*26+ASCII(@c)-64
            else
                return null
            
            set @str=stuff(@str,1,1,'')
        end
        return @i
    end
    goselect dbo.f_convert('')
    /*
    ----
    0
    */
    select dbo.f_convert('AC')
    /*
    ----
    29
    */
    select dbo.f_convert('12')
    /*
    ----
    NULL
    */drop function f_convert
    go
      

  2.   

    注意,以上函数并没有考虑bigint数据类型溢出的问题。
      

  3.   

    ALTER FUNCTION dbo.MyAscii
    (
    @inChar nvarchar(30)
    )
    RETURNS int
    AS
    BEGIN
    declare @m int,@len int,@tmpAscii int
    set @len = len(@inChar)
    set @m = 1
    set @tmpAscii=0
    while @m < = @Len
    begin

    set @tmpAscii=@tmpAscii*26 + (ascii(SUBSTRING(@inChar,@m,1))-64)
    set @m = @m + 1
    end

    return @tmpAsciiEND
    以上是转换成数字的。
    下面是从数字转换成字符的:
    ALTER FUNCTION dbo.MyVarchar
    (
    @InNumber int
    )
    RETURNS nvarchar(30)
    AS
    begin
    declare @mod int,@s nvarchar(30),@quotient int
    set @s=''
    set @quotient = @InNumber
    while @quotient>0 
    begin
    set @mod=@quotient%26
    set @quotient=@quotient/26
    if @mod=0
    begin
    set @mod=26
    set @quotient=@quotient-1
    end
    set @s =CHAR(@mod+64)+@s
    end
    return  @s
    end