Exotic use of User Defined Function

  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/column

  • randy sqlcentral

    SSC Rookie

    Points: 27

    You might be able to reduce a few instructions by using BETWEEN instead of x >= y AND x <= z.  So,   ...WHEN x BETWEEN y AND z.

    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.

    Enjoy

  • Baklabob

    SSChasing Mays

    Points: 622

    The email header for your article states: "User defined functions were added in SQL Server 7 and enhanced in SQL Server 2000...".

    I was not aware of any capabiltiy for user defined functions in SQL Server 7. Did I miss something?

     

  • Michael Irwin

    SSC Veteran

    Points: 295

    <>

    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

  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    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.

  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    I didn't wrote the header. Sorry. You are right that there is no user defined function in SQL Server 7.

  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    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.

  • Denis Oleinik

    SSC Journeyman

    Points: 76

    The column REALLY sensless. It's just an identity of a CHAR(3) type... THIS is much easier.

    declare @INT int

    set @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)

  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    As I pointed, it will be more than 999 number of records in a table.

    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.

    User defined function is set the value by default if NULL is inserted.

  • Jean Chevalier

    Old Hand

    Points: 345

    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.

    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 & 2^1 => giving either 0 or 1), entering 2 will do (ascii & 2^2 => 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.

    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.

    CREATE  function fn_char8hash ( @s char(8) , @p char(8) )
    returns int
    as
    begin
    if @s is null return @s
    set @s = replicate(' ', 8 - len(@s)) + @s
    declare @c char(1)
    declare @a smallint
    declare @e tinyint
    declare @l tinyint
    set @l = 0
    declare @r int
    set @r = 0
    declare @i tinyint
    set @i = 8
    while @i > 0
    begin
     set @e = cast(substring(@p, @i, 1) as tinyint)
     set @c = substring(@s, @i, 1)
     set @c = replace(upper(@c), '_', ' ')
     set @a = (power(2, @e) - 1) & (ascii(@c) - 
      case when @c < 'A' then 32 else 
      case when @e < 6 then 64 else 32 
      end end)
    -- print @a
     if @a > 0
     begin
      set @r = @a * power(2, @l) + @r
      set @l = @l + @e
     end
    -- print - @r
    -- print @l
     set @i = @i - 1
    end
    return @r
    end
    
  • Leo Peysakhovich

    Hall of Fame

    Points: 3880

    Cool way. And you got a good idea for the implementation.

     

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply