SQL: Is there a possibility to convert numbers (1,2,3,4…) to letters (A,B,C,D…)

前端 未结 5 1660
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 09:26

Is there a possibility to obtain letters (like A,B) instead of numbers (1,2) e.g. as a result of Dense_Rank function call(in MS Sql) ?

5条回答
  •  北海茫月
    2020-12-14 09:43

    I used this as a basis for my function to convert integers to base26 character strings

    DECLARE @Input integer  = 3000
    
    DECLARE @Value     integer  
    DECLARE @Quotient  integer      = 0
    DECLARE @Remainder integer      = 0
    DECLARE @Output    varchar(max) = ''
    
    DECLARE @BASE char(26) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    SET @Value = @Input
    
    WHILE @Value > 0 BEGIN
    
      SET @Quotient  = @Value / 26
      SET @Remainder = @Value % 26
    
      SET @Output = substring(@BASE,@Remainder,1) + @Output
    
      SELECT @Value = @Quotient
    
    END
    
    SELECT @Output --- DKJ
    

提交回复
热议问题