Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Exotic use of User Defined Function Expand / Collapse
Author
Message
Posted Saturday, July 24, 2004 5:20 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:49 PM
Points: 138, Visits: 270
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/column


Post #128055
Posted Tuesday, September 7, 2004 7:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 5, 2009 10:58 AM
Points: 1, Visits: 2

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

Post #135630
Posted Tuesday, September 7, 2004 10:35 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, October 27, 2005 7:23 AM
Points: 266, Visits: 1

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?

 




Post #135672
Posted Wednesday, September 8, 2004 12:20 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 21, 2014 7:17 AM
Points: 15, Visits: 55
<>

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
Post #135914
Posted Tuesday, September 14, 2004 5:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:49 PM
Points: 138, Visits: 270
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.


Post #136857
Posted Tuesday, September 14, 2004 6:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:49 PM
Points: 138, Visits: 270
I didn't wrote the header. Sorry. You are right that there is no user defined function in SQL Server 7.


Post #136859
Posted Tuesday, September 14, 2004 6:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:49 PM
Points: 138, Visits: 270

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.




Post #136861
Posted Wednesday, September 29, 2004 9:36 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 28, 2005 3:04 AM
Points: 50, Visits: 1
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)

Post #139292
Posted Wednesday, September 29, 2004 4:39 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:49 PM
Points: 138, Visits: 270

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.




Post #139370
Posted Thursday, February 24, 2005 3:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 7, 2014 6:45 AM
Points: 5, Visits: 91

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



Post #163689
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse