coolstoner coolstoner - 3 months ago 15
SQL Question

how to create auto increment column with characters and number like alphanumeric in sql server?

i have a table with primary key column which is auto-increment id of numerals starting from 1000..

now i have been asked to make a unique key for each records which starts with C20161, C20162, C20163 and so on the series goes on.....

so it is actually an alphanumeric key that auto-increments....

i would like to know how to implement the same in sql and also can i update the same to existing records?

is 'sequence' what i am looking for or something else?

Answer

You need COMPUTED COLUMN for this purpose,

CREATE TABLE TableName(
     ID INT IDENTITY(1,1) NOT NULL,
     NewColumnName AS 'C' + CAST(ID AS VARCHAR(30)),
     .... Other columns list here
)
Comments