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-Dedicated Group: General Forum Members Last Login: Today @ 7:39 AM Points: 34,554, Visits: 28,730
 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #930008
 Posted Friday, May 28, 2010 11:28 PM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 7:39 AM Points: 34,554, Visits: 28,730
 @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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #930010
 Posted Saturday, May 29, 2010 12:26 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 7:39 AM Points: 34,554, Visits: 28,730
 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #930011
 Posted Tuesday, June 01, 2010 9:12 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 7:39 AM Points: 34,554, Visits: 28,730
 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #930769
 Posted Tuesday, June 01, 2010 10:26 AM
 SSCrazy Group: General Forum Members Last Login: Wednesday, October 16, 2013 3:43 PM Points: 2,259, Visits: 5,393
 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 02, 2010 11:28 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, December 02, 2013 12:23 PM Points: 165, Visits: 318
 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 05, 2010 7:21 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 7:39 AM Points: 34,554, Visits: 28,730
 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #933146
 Posted Wednesday, October 02, 2013 12:42 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, October 02, 2013 12:46 AM Points: 2, Visits: 2
 Very Nice...thank you so much.
Post #1500664

 Permissions