How to set auto incremented, auto generated alphanumeric unique ID in sql server

In this article I am going to explain how to set auto incremented, auto generated alphanumeric unique ID in sql server.

How to set auto incremented, auto generated alphanumeric unique ID in sql server


Description:
I want to generate alphanumeric auto generated unique code for users. As you know primary key is always unique.

Implementation:
To generate unique code I have add new column to table. I have create a table Users

create TABLE [dbo].[Users](
            [Id] [int] IDENTITY(1,1) NOT NULL,
            [Firstname] [varchar](50) NULL,
            [Middlename] [varchar](50) NULL,
            [Lastname] [varchar](50) NULL,
            [Phonenumber] [int] NULL,
            [City] [varchar](50) NULL,
            [usercode] AS ('user' + right(('00000' + convert(varchar(5),[id])),5)) ,
) ON [PRIMARY]

Usercode column represent the alphanumeric auto generated column. Now insert some dummy record into the table to test it.



Post a Comment

0 Comments