Skip to content

TSQL 2012 – Generating letters with a SEQUENCE object

As shown in my previous posting on SEQUENCES, they are a user-defined object that generates a sequence of numeric values in Transact SQL 2012.  But what if you want to generate letters instead of numbers.

Here is a code example of a sequence used to generate letters (indirectly).


-- SEQUENCE
-- generating letters with a sequence.
--DROP SEQUENCE lettersSequence;
CREATE SEQUENCE lettersSequence
 AS tinyint
MINVALUE 65
 MAXVALUE 90
 CYCLE
 INCREMENT BY 1 ;

SELECT char(NEXT VALUE FOR lettersSequence) ;
SELECT char(NEXT VALUE FOR lettersSequence) ;
SELECT char(NEXT VALUE FOR lettersSequence) ;
SELECT char(NEXT VALUE FOR lettersSequence) ;
SELECT char(NEXT VALUE FOR lettersSequence) ;

Which generates the following output.

Just another cool trick with new features in sql server 2012.

 

For more info on sequences, take a look at my presentation for SQL Saturday 166 in Olympia WA.

More from Stedman Solutions:

SteveStedman5
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!

Leave a Reply

Your email address will not be published. Required fields are marked *