Incrementing Alpha character field sequentially

I have a field in a SQL table  called    textseq.          "AAA" is currently in the field.     I need to be able to add "1" to the field and make it "AAB"   and then add "1" to it and make it "AAC" until it gets to "AAZ" and then when I add "1" to it I need to increment the second "A" to "B" and then the field would be "ABA" and incrementing one to it will make it "ABB" and so forth.  
The reason why I need this is we have a unique serial number that has to be generated I have to be able to increment and then store it because I tie a piece of inventory to it.   The number is actually   "AAA001" and what they are manually doing is putting the serial number on a speadsheet and adding one to it and when they eventually get to AAA999 they increment the "AAA" to "AAB" and reset the "999" back to "000".  It is constantly growing.

I just didn't know if there was a way to do it in SQL or if I had to actually do it in VB.    I thought about it being two fields that I would bring back together to make a complete unique serial number.   I am just not sure how to increment the text letters by one character.
Who is Participating?
tliottaConnect With a Mentor Commented:
SQL doesn't know how to do this. Nor does any programming language I've ever heard of. You just don't "add 1 to a letter". But that doesn't mean it can't be done; it just needs to be coded.

Technically, it's just base(26) arithmetic, and it might be implemented by referencing a 'number line' consisting of the alphabet. E.g., the letter 'A' would be zero in base(26) and 'Z' would be 25. One simple function to give those results would be

Open in new window

which gives zero and

Open in new window

which gives 25.

In order to handle 'carry-over', you'd use a second string of the alphabet with an extra 'A' at the end, i.e., 'ABCDEFGHIJKLMNOPQRSTUVWXYZA'. Apparently, you'll never have a carry-over greater than 1, so the most that could happen would be adding 1 to 'Z' to reach 'A'.

Simply remembering how addition works, in combination with the concepts of "number line" plus base(26) should lead to a function that works correctly.

Scott PletcherSenior DBACommented:
I'd "fudge" around actually incrementing the alpha string.

Instead, create a table with the alpha strings and numerical code equivalents.  Look up the current string value -- say AAZ -- to get the code, add 1 to the code, then look up the equivalent string.  You have a simpler process -- a numeric increment, which is trivial on any computer -- and you can easily exactly control the sequence of alpha values.

>>  I thought about it being two fields that I would bring back together to make a complete unique serial number. <<

Yes, absolutely.  Have each part of the number a separate column, and use a computed column to return the combination to use as the serial number.

For example:
serial_number_prefix char(3)
serial_number_suffix char(3)
serial_number AS CAST(serial_number_prefix + serial_number_suffix AS char(6))
Russell FoxDatabase DeveloperCommented:
Or <ahem> lie to them: if "ACB" doesn't really mean anything to anyone, I'd just create an identity column and seed it to 100000 os it's the same "length" that they're used to. If you tell them that you can automate the whole thing but only if they give up the meaningless alphas, they might just go along with it.
All Courses

From novice to tech pro — start learning today.