May 15, 2009

AutoGenerate an AlphaNumeric Sequence in SQL Server

I was recently solving a requirement of a client where the client needed a Unique Alphanumeric Sequence Number to be generated using the following business rules:

UniqueID = Code (TT) + Current Datetime + ID

Here's how I generated the UniqueID



ID  int, CircuitName varchar(10),

UniqueID  AS 'TT' + REPLACE(CONVERT(varchar, GETDATE(),101),'/','')

+ REPLACE(CONVERT(varchar, GETDATE(),108),':',''

+ CAST(ID as varchar(10))




SELECT 1, 'Circuit 1' UNION ALL

SELECT 2, 'Circuit 2' UNION ALL

SELECT 3, 'Circuit 3' UNION ALL

SELECT 4, 'Circuit 4' UNION ALL

SELECT 5, 'Circuit 5' UNION ALL

SELECT 6, 'Circuit 6' UNION ALL

SELECT 7, 'Circuit 7' UNION ALL

SELECT 8, 'Circuit 8' UNION ALL

SELECT 9, 'Circuit 9' UNION ALL

SELECT 10, 'Circuit 10'




ID    CircuitName    UniqueID

1    Circuit 1    TT051220091517481

2    Circuit 2    TT051220091517482

3    Circuit 3    TT051220091517483

4    Circuit 4    TT051220091517484

5    Circuit 5    TT051220091517485

6    Circuit 6    TT051220091517486

7    Circuit 7    TT051220091517487

8    Circuit 8    TT051220091517488

9    Circuit 9    TT051220091517489

10    Circuit 10    TT0512200915174810

Did you like this post?
kick it on
subscribe via rss subscribe via e-mail
print this post follow me on twitter

About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal



3 Responses to "AutoGenerate an AlphaNumeric Sequence in SQL Server"
  1. Tumo Jobas said...
    October 7, 2010 at 12:38 AM

    excellent topic. Can I use this method to generate an id of integers but not in any sequence, just random 10 digit numbers?

  2. Suprotim Agarwal said...
    October 7, 2010 at 12:54 AM

    Tumo yes there is a way that I use using the NEWID(). Just wait for a couple of hours and I will update this post with a solution.

  3. Suprotim Agarwal said...
    October 7, 2010 at 6:07 AM

    Here's the solution you are looking out for AutoGenerate Numeric ID’s in SQL Server with No Sequence


Copyright © 2009-2016 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions