Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Auto Increment Alphabet


Auto Increment Alphabet

Author
Message
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
Alright... here you go. First, we'll demo a way to accomplish the task and then I'll show in the post following this one why this type of numbering should NEVER be done.

The way the following code works is that it actually uses an IDENTITY column to provide a true auto-incrementing capability instead of having to do any manual sequencing. That's important because instead of using a bunch of self joins to an "external number generator" or sequencer of any kind, we can use a Computed Column, instead.

I still think this type of "numbering" is a huge mistake (see the next post down for why).

--===== Create a table with a real auto-incrementing column and
-- a persisted formula to convert it to Base 26 automatically.
-- The auto-incrementing column is "Zero Based" to keep things simple.
-- Max value = 26^7-1 = 8,031,810,175 > Largest INT
CREATE TABLE #MyHead
(
MyHeadID INT IDENTITY(0,1) NOT NULL,
Base26ID AS CHAR(MyHeadID/308915776%26+65) --26^6
+ CHAR(MyHeadID/11881376%26+65) --26^5
+ CHAR(MyHeadID/456976%26+65) --26^4
+ CHAR(MyHeadID/17576%26+65) --26^3
+ CHAR(MyHeadID/676%26+65) --26^2
+ CHAR(MyHeadID/26%26+65) --26^1
+ CHAR(MyHeadID%26+65) --26^0
PERSISTED NOT NULL,
SomeString VARCHAR(36) NOT NULL
)

--===== Add a million+1 rows of "something" to the table
-- just to prove it works.
INSERT INTO #MyHead
(SomeString)
SELECT TOP 1000001
NEWID() AS SomeString
FROM Master.sys.All_Columns ac1
CROSS JOIN Master.sys.All_Columns ac2

--===== Show what's in the table
SELECT *
FROM #MyHead




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
@Ankit (or anyone else who thinks this type of "numbering" is a good idea),

Let me tell you why BASE 26 "numbering" like this is SO bad that it'll likely get your company sued... try the following code with your choice of classic "3 and 4 letter" swear words and see what you get...


SELECT * FROM #MyHead WHERE Base26ID LIKE '%CRAP%'



Every soccer mom and little old lady in the world will be out to get you and your company if these numbers ever are exposed publicly.;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
Even though I still don't like this kind of "numbering", let's try things in a slightly different manner. If we remove all the vowels from the alphabet, it's a wee bit more difficult to spell swear words.

First, build the following scalar function. It's got to be scalar because we're still going to use it in a computed column. It can't be persisted, either, because it ends up being non-deterministic in this case and I haven't tried to figure out how to make it so because it actually runs faster than the previous example...

 CREATE FUNCTION dbo.Base21
--===== This function accepts an integer and returns mostly harmless
-- "numbering" using letters with all vowels removed.
-- Jeff Moden
(@Integer INT)
RETURNS VARCHAR(8) AS
BEGIN
DECLARE @Return VARCHAR(8)
;

WITH
cteAllowed AS
(
SELECT 'BCDFGHJKLMNPQRSTVWXYZ' AS Letters
)
SELECT @Return =
SUBSTRING(Letters, @Integer/1801088541%21+1, 1) --21^7
+ SUBSTRING(Letters, @Integer/85766121%21+1, 1) --21^6
+ SUBSTRING(Letters, @Integer/4084101%21+1, 1) --21^5
+ SUBSTRING(Letters, @Integer/194481%21+1, 1) --21^4
+ SUBSTRING(Letters, @Integer/9261%21+1, 1) --21^3
+ SUBSTRING(Letters, @Integer/441%21+1, 1) --21^2
+ SUBSTRING(Letters, @Integer/21%21+1, 1) --21^1
+ SUBSTRING(Letters, @Integer%21+1, 1) --21^0
FROM cteAllowed
;
RETURN @Return
END
GO



Now we can do this without all the swear words...


--===== Create a table with a real auto-incrementing column and
-- a persisted formula to convert it to Base 21 (no vowels) automatically.
-- The auto-incrementing column is "Zero Based" to keep things simple.
CREATE TABLE #MyHead
(
MyHeadID INT IDENTITY(0,1) NOT NULL,
Base21ID AS dbo.Base21(MyHeadID)
,
SomeString VARCHAR(36) NOT NULL
)

--===== Add a million+1 rows of "something" to the table
-- just to prove it works.
INSERT INTO #MyHead
(SomeString)
SELECT TOP 1000000
NEWID() AS SomeString
FROM Master.sys.All_Columns ac1
CROSS JOIN Master.sys.All_Columns ac2

--===== Show what's in the table
SELECT *
FROM #MyHead




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
The lack of feedback on this post has me worried that the OP is actually using the alphabetic increment in the unfiltered mode. Please save your company a lawsuit and don't do it.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2317 Visits: 5545
Goodness me, i lost track of this thread and now i saw that this alpha-numbering is a huge swear-word dump-yard.. hmmm,as u said Jeff, it will invite lawsuits.. lets wait for the OP to post back his thoughts...
Ankit Mathur-481681
Ankit Mathur-481681
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 328
Hi Guys,

I'm so sorry I couldn't pitch in before.


Wayne - Well, you did say you only needed it to get to AAAA. ZZZZ is much higher! ;-)


@Wayne: Desires are insatiable :-P


Jeff - The lack of feedback on this post has me worried that the OP is actually using the alphabetic increment in the unfiltered mode. Please save your company a lawsuit and don't do it.


@Jeff: Yep .. you are right Jeff. Frankly, I didn't thought all that far. Your posts are enlightening.

As for lack on feedback on your attempts. I hope it doesn't put you both off but pardon my knowledge of Base 26 numbers. Its a new concept for me as I haven't used or, seen it being used anywhere so far & all this while I was & am actually trying to gain knowledge of Base 26 numbering system so that I can actually understand & implement your code.

Ankit
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45203 Visits: 39925
Thanks for the feedback, Ankit...

There's a fair bit of complexity to using a base 26 "numbering" system like this even if you don't take into account all the swear words that can be realized. I still recommend avoiding it if you can.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
rajesh_somvanshi
rajesh_somvanshi
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
Very Nice...thank you so much.
webridges
webridges
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
I needed something to increment alphabetically for labeling revisions and stumbled across this thread. I tried several of the "mathematical" ways at attempting BASE26, but I could never get it to work since I needed A to be 1 and a leading blank/null to be 0 so that Z would roll to AA, etc. So I just went at it from another angle of simply dealing with the characters themselves.

In case anyone else has the need, this is what I ended up with: (Could be cleaned up, but this leaves it somewhat readable.)

DECLARE @Revision VARCHAR(20) = 'Z'; -- String to increment
DECLARE @Characters VARCHAR(50) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; -- Characters to use

DECLARE @CarryOver INT = 0;
DECLARE @Interation INT = 0;
DECLARE @CharIndex INT;
WHILE @CarryOver > 0
OR @Interation = 0
BEGIN
IF @Interation >= LEN(@Revision)
BEGIN
SET @Revision = SUBSTRING(@Characters, @CarryOver, 1) + @Revision;
SET @CarryOver = @CarryOver / LEN(@Characters);
END;
ELSE
BEGIN
SET @CharIndex = CHARINDEX(SUBSTRING(REVERSE(@Revision), @Interation + 1, 1), @Characters) + 1;
SET @Revision = STUFF(@Revision, LEN(@Revision) - @Interation, 1, SUBSTRING(@Characters, @CharIndex % LEN(@Characters), 1));
SET @CarryOver = @CharIndex / LEN(@Characters);
END;
SET @Interation = @Interation + 1;
END;
SELECT @Revision;


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search