Link to home
Start Free TrialLog in
Avatar of Patrick Miller
Patrick MillerFlag for United States of America

asked on

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.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

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))
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.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_276102
Member_2_276102

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial