Please disable your adblock and script blockers to view this page
 

Auto generate auto incremented unique alphanumeric id or number in sql server

Introduction: In this article I am going to share how to auto generate unique auto incremented alphanumeric code or number and store in sql server table while inserting any record.


Description:  While working on project I got the requirement to auto generate 8 character employee code having 3 characters "EMP" as a prefix and 5 digits number and it should be auto incremented whenever a new record in inserted in the table.  

As we know primary key column of a table is always unique so we can use that column value to auto generate employee code. I made the "EmpCode" column a computed column and specified the formula to generate the auto incremented alphanumeric number.  Computed columns are derived columns based on other existing columns in the same table or we can say a computed column is computed from an expression/formula that can use another column or columns in the same table.

Implementation: Let’s generate alphanumeric employee code automatically.

CREATE TABLE tbEmployee
(
                EmpId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
                EmpCode AS 'EMP' + RIGHT('0000'+ CONVERT(VARCHAR(5),EmpId),5) PERSISTED,
                EmployeeName VARCHAR(50),
                Age        INT,
                Gender VARCHAR(10)
)
--INSERT RECORDS IN TABLE
INSERT INTO tbEmployee (EmployeeName,Age,Gender)VALUES
('Mayank',25,'Male'),
('Ankita',24,'Female'),
('Anuj',26,'Male'),
('Jaswinder',25,'Male'),
('Simran',25,'Female') 

--CHECK INSERTED RECORD
SELECT * FROM tbEmployee

Result will be: 
EmpId
EmpCode
EmployeeName
Age
Gender
1
EMP00001
Mayank
25
Male
2
EMP00002
Ankita
24
Female
3
EMP00003
Anuj
26
Male
4
EMP00004
Jaswinder
25
Male
5
EMP00005
Simran
25
Female
  Explanation: When the very first employee record is saved in the table, the value of primary key column "EmpId" will be 1 so the computed column "EmpCode" will be auto generated as EMP00001 and auto stored in table, similarly when 105th employee record is saved, "EmpCode" will be generated as EMP00105, for 1580th record it will be EMP01580 and for 10000th record it will generate EMP10000.

To test whether it actually works or not,execute the following script 

--CREATE A TEMPORARY TABLE
CREATE TABLE #Temp
(
                Id INT NOT NULL IDENTITY(1,1),
                AutoId AS 'ABC' + RIGHT('0000'+ CONVERT(VARCHAR(5),Id),5)
)
--INSERT DEFAULT 10000 RECORDS AT ONCE
INSERT INTO #Temp DEFAULT VALUES;
GO 10000

--CHECK INSERTED RECORD
SELECT * FROM #Temp

Result will be as:
 Auto generate auto incremented unique alphanumeric id or number in sql server

--DROP TEMPORARY TABLE

DROP TABLE #Temp

Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Previous
Next Post »

13 comments

Click here for comments
madhu reddy
admin
August 08, 2015 ×

Thank u sir ,its very useful us

Reply
avatar
August 11, 2015 ×

I am glad you found this article helpful..stay connected and keep reading for more useful updates

Reply
avatar
August 21, 2015 ×

Very nice. It helped me.

Reply
avatar
Anonymous
admin
August 29, 2015 ×

OWESOME

Reply
avatar
September 02, 2015 ×

thanks for your feedback..stay connected and keep reading

Reply
avatar
October 04, 2015 ×

Your welcome harmesh..stay connected for more useful updates.

Reply
avatar
Anonymous
admin
December 28, 2015 ×

Thanks for this article...it helped me..saved my time

Reply
avatar
February 08, 2016 ×

Thanks for you feedback..I am glad you liked this article..stay connected and keep reading...

Reply
avatar
March 01, 2016 ×

Does it handle concurrency issue?

Reply
avatar
John Cox
admin
March 18, 2016 ×

Nice work! I'm not a Database specialist, but I can use the information presented here.

Reply
avatar
March 26, 2016 ×

Thanks for your valuable feedback.

Reply
avatar
March 28, 2016 ×

Thanks i found this really helpful.
Also, while implementing this in my project i need it to generate a code consisting of;
- a prefix string 'REQ'
- Date it was created i.e 2016-03-11
- 3 digits number that resets every day

Any advice on how to go about that?
Once Again thanks for this post i found it really helpful.

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon