Solved

Incrementing Alpha character field sequentially

Posted on 2014-07-25
3
425 Views
Last Modified: 2014-08-12
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.
0
Comment
Question by:patrickmiller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40220454
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))
0
 
LVL 13

Expert Comment

by:Russell Fox
ID: 40220629
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.
0
 
LVL 27

Accepted Solution

by:
tliotta earned 500 total points
ID: 40252980
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
(locate('A', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')-1)

Open in new window

which gives zero and
(locate('Z', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')-1)

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.

Tom
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question