Solved

Incrementing Alpha character field sequentially

Posted on 2014-07-25
3
371 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
3 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now