﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Leo Peysakhovich / Article Discussions / Article Discussions by Author  / Exotic use of User Defined Function / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 02:46:29 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;Cool way. And you got a good idea for the implementation.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Thu, 24 Feb 2005 15:03:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;This reminds me of a function I had to write recently, to convert alphanumeric keys to numeric keys.  The company had char(8) keys as '3AJPUL68' and desired to map the alphanumeric space to a numeric space, in order to start using numeric keys.  I've written a function that takes a string and makes up a numeric hash for it.&lt;/P&gt;&lt;P&gt;The resolution parameter @p controls how many bits of information are kept, at each position of the string.  Entering 0 will suppress the position, 1 will do (ascii &amp;amp; 2^1 =&amp;gt; giving either 0 or 1), entering 2 will do (ascii &amp;amp; 2^2 =&amp;gt; resulting in either 0, 1, 2, or 3), etc, up to 6, being the highest resolution, which would make the ascii code correspond to a number between 0 and 63.  As the function iterates through the string positions, from right to left, it shifts the intermediate left results before adding the next hash value.&lt;/P&gt;&lt;P&gt;By setting the resolution parameter @p to '00016666' you'd always get integers in return (because the sum of all bits is less than 32), that's what we did, because the leftmost characters were almost always for us non-significant.  You may need to change the return value to bigint if you need a higher resolution for the left-most positions.&lt;/P&gt;&lt;PRE&gt;CREATE  function fn_char8hash ( @s char(8) , @p char(8) )returns intasbeginif @s is null return @s&lt;/PRE&gt;&lt;PRE&gt;set @s = replicate(' ', 8 - len(@s)) + @sdeclare @c char(1)declare @a smallint&lt;/PRE&gt;&lt;PRE&gt;declare @e tinyintdeclare @l tinyintset @l = 0&lt;/PRE&gt;&lt;PRE&gt;declare @r intset @r = 0&lt;/PRE&gt;&lt;PRE&gt;declare @i tinyintset @i = 8while @i &amp;gt; 0begin set @e = cast(substring(@p, @i, 1) as tinyint) set @c = substring(@s, @i, 1)&lt;/PRE&gt;&lt;PRE&gt; set @c = replace(upper(@c), '_', ' ') set @a = (power(2, @e) - 1) &amp;amp; (ascii(@c) -   case when @c &amp;lt; 'A' then 32 else   case when @e &amp;lt; 6 then 64 else 32   end end)-- print @a if @a &amp;gt; 0 begin  set @r = @a * power(2, @l) + @r  set @l = @l + @e end-- print - @r-- print @l&lt;/PRE&gt;&lt;PRE&gt; set @i = @i - 1end&lt;/PRE&gt;&lt;PRE&gt;return @r&lt;/PRE&gt;&lt;PRE&gt;end&lt;/PRE&gt;</description><pubDate>Thu, 24 Feb 2005 03:00:00 GMT</pubDate><dc:creator>Jean Chevalier</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;As I pointed, it will be more than 999 number of records in a table.&lt;/P&gt;&lt;P&gt;Column data must be assigned automatically by the database. No one wants to rebuid the applications. Anyway any mechanism must create a value and place it as default for the column.&lt;/P&gt;&lt;P&gt;User defined function is set the value by default if NULL is inserted.&lt;/P&gt;</description><pubDate>Wed, 29 Sep 2004 16:39:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>The column REALLY sensless. It's just an identity of a CHAR(3) type... THIS is much easier. declare @INT intset @INT = 32768 --(smallint)declare @VB varbinary(3)set @VB = cast( @INT as varbinary(3))select @VB,CHAR(ASCII(SUBSTRING(@VB, 1, 1)) + 33) +CHAR(ASCII(SUBSTRING(@VB, 2, 1)) + 33) +CHAR(ASCII(SUBSTRING(@VB, 3, 1)) + 33)</description><pubDate>Wed, 29 Sep 2004 09:36:00 GMT</pubDate><dc:creator>Denis Oleinik</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;You may be right. I will try it. But the article was not written to show the best code, but to show the idea of how to use user defined function for the case. &lt;/P&gt;</description><pubDate>Tue, 14 Sep 2004 06:03:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>I didn't wrote the header. Sorry. You are right that there is no user defined function in SQL Server 7. </description><pubDate>Tue, 14 Sep 2004 06:01:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>I can't use identity column. The column should be char(3) (or int with 3 digits). Database is growing dynamically. So, identity column has only 999 combinations. I need some where close to 40,000.</description><pubDate>Tue, 14 Sep 2004 05:58:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;&lt;We can’t use identity column for the key generation because the number of records in the table can be more then 999 but less then 30,000, which means that the function should use combination of letters and numbers.&gt;&gt;Why not use an identity column ? There are plenty of number and letters  available ! Leave the identity column as anumber until it is needed, and then convert it. If you only use numbers and upper-case letters you have 36 (base 36 !), so your range is from 1 to (36^3 + 36^2 + 36) (about 48K). Using lower-case letters too gets you to 240K+ ! Surely that would be sufficient !Mike</description><pubDate>Wed, 08 Sep 2004 12:20:00 GMT</pubDate><dc:creator>Michael Irwin</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;The email header for your article states: "User defined functions were added in SQL Server 7 and enhanced in SQL Server 2000...".&lt;/P&gt;&lt;P&gt;I was not aware of any capabiltiy for user defined functions in SQL Server 7. Did I miss something?&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 07 Sep 2004 10:35:00 GMT</pubDate><dc:creator>Baklabob</dc:creator></item><item><title>RE: Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>&lt;P&gt;You might be able to reduce a few instructions by using BETWEEN instead of x &amp;gt;= y AND x &amp;lt;= z.  So,   ...WHEN x BETWEEN y AND z.&lt;/P&gt;&lt;P&gt;There seems to be an oportunity to simply write a BASE 10 to BASE 36 conversion routine.  I don't know if there is a more straightforward conversion than what you've preposed here but it might be worth a thought.&lt;/P&gt;&lt;P&gt;Enjoy&lt;/P&gt;</description><pubDate>Tue, 07 Sep 2004 07:46:00 GMT</pubDate><dc:creator>randy sqlcentral</dc:creator></item><item><title>Exotic use of User Defined Function</title><link>http://www.sqlservercentral.com/Forums/Topic128055-163-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/lPeysakhovich/exoticuseofuserdefinedfunction.asp&gt;http://www.sqlservercentral.com/column</description><pubDate>Sat, 24 Jul 2004 17:20:00 GMT</pubDate><dc:creator>Leo Peysakhovich</dc:creator></item></channel></rss>