SQL 26字母进位SQL

2014-11-24 13:24:01 · 作者: · 浏览: 0
SQL 26字母进位SQL
基本思路,把需要进行加法运算的字符转换为10进制后,进行进位10进制的加法,加法完成的结果在转换为26进制字符表示方法,SQL 如下,如果有更好的思路或者方法,可以留言!
[sql] 
--输入表达式  
declare @Expression nvarchar(max)  
set @Expression='AA'  
--需要加多少位  
declare @Count int  
set @Count = 0  
---------------------  
    --字母表达式长度  
    declare @length int  
    set @length = LEN(@Expression)  
    --转为10进制后的数字  
    declare @CharValue int  
    set @CharValue =0  
    --循环每个字母位转为10进制  
    while @length > 0  
    begin  
        set @CharValue += (ASCII(SUBSTRING(@Expression,@length,1))-64)*POWER(26,@length-1)  
        set @length -=1  
    end  
    --10进制加法  
    set @CharValue += @Count  
    --新的字符表达式位数  
    declare @NewCharLength int  
    --计算加完后的十进制转为字母26进制的位数  
    set @NewCharLength = LOG(@CharValue)/LOG(26)  
    --加完后表达式  
    declare @NewChar nvarchar(max)  
    set @NewChar = ''  
      
    while @NewCharLength >
= 0 begin declare @IndexNum int set @IndexNum = @CharValue/POWER(26,@NewCharLength) set @NewChar += SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ', @IndexNum,1) select @CharValue,POWER(26,@NewCharLength),@IndexNum set @CharValue -= @IndexNum * POWER(26,@NewCharLength) set @NewCharLength -= 1 end select @NewChar