Generate Identity Column As Sequence String In SQL Server 2008

This article describes how to create alphanumeric identity column in SQL Server.
  • 4199

Introduction

This article describes how to create alphanumeric identity column in SQL Server.

Sometimes there is a requirement to create identity column not as a numeric value but a alpha numeric value or in some cases  custom identity column.

Let us suppose, you have a requirement to generate users id automatically. It should be like a1,a2,a3,a4...b1,b2,b3,b4...and so on. Numbers should be alphanumeric.

Write following code snippet.

declare @character char(1) , @integer_value varchar(1000)

declare @value int =97, @secondvalue int =1

while @value <= 122

begin

      set @secondvalue=1

      while @secondvalue < 100

       begin

                set @character = char(@value)

                set @integer_value = convert(char,@secondvalue)

                set @integer_value= @character + @integer_value

                print @integer_value

                set @secondvalue = @secondvalue+1

        end

set @value=@value+1

end

Run the above code and see string sequence is generated.

Output

AlphanumericIdentityColumn.jpg

© 2020 DotNetHeaven. All rights reserved.