Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Auto Increment Alphabet Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, May 28, 2010 10:54 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 11:36 AM Points: 42,082, Visits: 39,474
 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." Helpful Links:How to post code problemsHow to post performance problems
Post #930008
 Posted Friday, May 28, 2010 11:28 PM
 SSC-Forever Group: General Forum Members Last Login: Today @ 11:36 AM Points: 42,082, Visits: 39,474
 @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." Helpful Links:How to post code problemsHow to post performance problems
Post #930010
 Posted Saturday, May 29, 2010 12:26 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 11:36 AM Points: 42,082, Visits: 39,474
 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 BEGINDECLARE @Return VARCHAR(8);WITHcteAllowed 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 ENDGO`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." Helpful Links:How to post code problemsHow to post performance problems
Post #930011
 Posted Tuesday, June 1, 2010 9:12 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 11:36 AM Points: 42,082, Visits: 39,474
 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." Helpful Links:How to post code problemsHow to post performance problems
Post #930769
 Posted Tuesday, June 1, 2010 10:26 AM
 SSCrazy Group: General Forum Members Last Login: Saturday, November 19, 2016 1:44 PM Points: 2,271, Visits: 5,545
 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...
Post #930842
 Posted Wednesday, June 2, 2010 11:28 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Saturday, July 5, 2014 2:22 AM Points: 167, 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 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
Post #931846
 Posted Saturday, June 5, 2010 7:21 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 11:36 AM Points: 42,082, Visits: 39,474
 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." Helpful Links:How to post code problemsHow to post performance problems
Post #933146
 Posted Wednesday, October 2, 2013 12:42 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, October 2, 2013 12:46 AM Points: 2, Visits: 2
 Very Nice...thank you so much.
Post #1500664
 Posted Wednesday, March 9, 2016 8:46 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, March 9, 2016 8:47 AM 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 incrementDECLARE @Characters VARCHAR(50) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; -- Characters to useDECLARE @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;`
Post #1767967

 Permissions