November 16, 2005 at 8:21 pm
I am working with EVENT_LOCATOR varchar(40) value. An example EVENT_LOCATOR would be "2005-AAAHNY". The "2005-" first 5 characters of the string would never be affected by incrementing the string; the last 6 characters would be. Acceptable values for the last 6 characters are uppercase A through uppercase Z. So, for example:
"2005-AAAHNY" incremented once would become "2005-AAAHNZ". Since Z is the highest you can go for each alpha character, "2005-AAAHNZ" incremented one would become "2005-AAAHOA" Further incrementation would produce
2005-AAAHOB
2005-AAAHOC
2005-AAAHOD
etc.
What I would like to be able to do is to increment the string using SQL. A UDF, for example, that when given "2005-AAAHNZ", would return "2005-AAAHOA".
Any ideas?
November 17, 2005 at 3:03 am
This would be a solution to your problem. (Probably some extra error handling will be needed, to check that it only accepts upper case chars, and so on..) But the recursive char incrementation should work fine.
Regards,
Jan
IF EXISTS (SELECT *
FROM sysobjects
WHERE name = N'fnIncrementString')
DROP FUNCTION fnIncrementString
GO
CREATE FUNCTION fnIncrementString(@sChar varchar(6))
RETURNS varchar(6)
AS
BEGIN
DECLARE @nLastChar smallint
DECLARE @sReturn varchar(6)
SELECT @nLastChar = ASCII(SUBSTRING(@sChar, LEN(@sChar),1))
IF @nLastChar = 90
SELECT @sReturn = dbo.fnIncrementString(SUBSTRING(@sChar, 1, LEN(@sChar) - 1)) + 'A'
ELSE
SELECT @sReturn = SUBSTRING(@sChar, 1, LEN(@sChar) - 1) + CHAR(@nLastChar + 1)
RETURN @sReturn
END
GO
-- =============================================
-- Example to execute function
-- =============================================
SELECT dbo.fnIncrementString('AAAHOC')
SELECT dbo.fnIncrementString('AAAHOZ')
SELECT dbo.fnIncrementString('AZZZZZ')
November 17, 2005 at 7:43 am
This is a great place for SQLCLR in SQL Server 2005.
November 18, 2005 at 6:45 am
Great work Jan,
I had some issues with the T-SQL so changed the function a little:
CREATE FUNCTION fn_IncString (@String varchar(6))
RETURNS varchar(6)
AS
BEGIN
DECLARE @C smallint,@Return varchar(6)
SELECT @C=ASCII(RIGHT(@String,1))
IF @C=90
SELECT @Return=dbo.fn_IncString(LEFT(@String,LEN(@String)-1))+'A'
ELSE
SELECT @Return=LEFT(@String,LEN(@String)-1)+CHAR(@C+1)
RETURN @Return
END
Andy
November 18, 2005 at 6:59 am
Thanks, guys!. I didn't know you could do recursion within a function in TSQL.
November 18, 2005 at 7:38 pm
Brilliant use of recursion guys! Absolutely awesome.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2015 at 10:14 am
Great job, Jan-155192. I'm going to modify to include numerics; ie. 'AAAAAZ' will increment to 'AAAAA0' and 'AAAAA9' would increment to 'AAAABA'. I'll update the post after I'm done for anyone that may also need.
Thank you
February 2, 2015 at 11:51 am
My last response on this was nearly a decade ago. I no longer believe that recursion of a scalar function would be the right way to go nor do I believe that incrementing alpha values are, either. You can spell a huge number of highly offensive words in just 4 letters, never mind six.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2015 at 12:07 pm
K2mission (2/2/2015)
Great job, Jan-155192. I'm going to modify to include numerics; ie. 'AAAAAZ' will increment to 'AAAAA0' and 'AAAAA9' would increment to 'AAAABA'. I'll update the post after I'm done for anyone that may also need.Thank you
Hmm, you're using sort of a mixed up Base 36 which would go 0-9 then A-Z.
February 2, 2015 at 12:34 pm
K2mission (2/2/2015)
Great job, Jan-155192. I'm going to modify to include numerics; ie. 'AAAAAZ' will increment to 'AAAAA0' and 'AAAAA9' would increment to 'AAAABA'. I'll update the post after I'm done for anyone that may also need.Thank you
I have never figured out a situation where this type of "incrementing" makes any sense. What is the point? Are you going to sort these values? Is there some reason you need to use such a strange value? What is wrong with an incrementing number value?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 2, 2015 at 12:57 pm
Jeff Moden (2/2/2015)
My last response on this was nearly a decade ago. I no longer believe that recursion of a scalar function would be the right way to go nor do I believe that incrementing alpha values are, either. You can spell a huge number of highly offensive words in just 4 letters, never mind six.
No fair. I only know two of them.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 2, 2015 at 1:13 pm
ChrisM@home (2/2/2015)
Jeff Moden (2/2/2015)
My last response on this was nearly a decade ago. I no longer believe that recursion of a scalar function would be the right way to go nor do I believe that incrementing alpha values are, either. You can spell a huge number of highly offensive words in just 4 letters, never mind six.No fair. I only know two of them.
Work and Debt?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 2, 2015 at 4:15 pm
The only place I've used Base36 is to jam a lot of numbers into a short barcode. Just think how long a binary number is compared to a hex number.
And the second reason to use Base36 is for the four-letter words you can make!
February 9, 2015 at 8:28 am
Sean,
The thought was to allow for as many 6-character combinations as possible. Using the alphabet along with numbers offered 2,176,782,336 combinations whereas numbers alone would only offer 1,000,000 (zero-base). However, in light of the many spellings that could be fabricated from this arrangement (thanks to all for the feedback and laughs), it's best that other solutions are utilized. Our team had restrictions in what was permitted to be sent to a vendor. We were able to work out another format that better represented the needs of each party.
Thanks,
Steve
February 9, 2015 at 9:21 am
K2mission (2/9/2015)
Sean,The thought was to allow for as many 6-character combinations as possible. Using the alphabet along with numbers offered 2,176,782,336 combinations whereas numbers alone would only offer 1,000,000 (zero-base). However, in light of the many spellings that could be fabricated from this arrangement (thanks to all for the feedback and laughs), it's best that other solutions are utilized. Our team had restrictions in what was permitted to be sent to a vendor. We were able to work out another format that better represented the needs of each party.
Thanks,
Steve
At least remove the vowels and the letter "Y" from the mix so that you don't spell out so many offensive words in the process.
To be honest though, it only takes 4 bytes to get numbers from 0 to over two billion. Have a display limit of six bytes is a pretty silly requirement you've been given computationally speaking.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply