Ask the Expert

Generate a range of two-character codes

I want to generate a range of auto-incremented characters between two alphanumeric values. For example, AA to BA should generate AA, AB, AC, AD,...BA. Your help in this regard is highly appreciated.

    Requires Free Membership to View

First of all, these aren't going to be "auto"-incremented. If you're hoping to have the database generate these codes automatically, it's not going to happen. The best you can do is generate these codes yourself. Start by creating a LETTERS table:

create table letters
( letter char(1) not null primary key
);
insert into letters values
 ('A'),('B'),('C'),('D'),('E'),('F'),('G')
,('H'),('I'),('J'),('K'),('L'),('M'),('N')
,('O'),('P'),('Q'),('R'),('S'),('T'),('U')
,('V'),('W'),('X'),('Y'),('Z');

Now you can easily generate any range of codes like this:

select t1.letter||t2.letter
  from letters as t1
cross
  join letters as t2
 where t1.letter||t2.letter
       between 'AA' and 'BA'

Simple, eh?

This was first published in July 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: