Solved

Incrementing Alpha character field sequentially

Posted on 2014-07-25
3
388 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

773 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