SQL Server 2012 introduced a Sequence object with lots of functionality, but unfortunately we can only create Sequence Object with Number data-types.
We can create a Sequence object for a list of this data-types:
But in some situation we also require alphanumeric Sequence Number and for that we should apply special FORMAT on the value of Sequence Number.
Below is a full demonstration to FORMAT a Sequence value and make it alphanumeric.
First, Create sample table:
CREATE TABLE dbo.tbl_TestVarcharSequence
,CONSTRAINT pk_tbl_TestVarcharSequence_VarcharID PRIMARY KEY(VarcharID)
Create a Sequence object:
CREATE SEQUENCE dbo.seq_TestVarcharSequenceNumber AS
INT START WITH 1
INCREMENT BY 1;
Create Sequence constraint using FORMAT function:
ALTER TABLE dbo.tbl_TestVarcharSequence
ADD CONSTRAINT seq_tbl_TestVarcharSequence_VarcharID DEFAULT
FORMAT((NEXT VALUE FOR dbo.seq_TestVarcharSequenceNumber),'ABC000000#')
Insert few sample records:
INSERT INTO dbo.tbl_TestVarcharSequence (Name)
SELECT * FROM dbo.tbl_TestVarcharSequence
You can find Alphanumeric Sequence Number in the above result.
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.