SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Exotic use of User Defined Function


Exotic use of User Defined Function

Author
Message
Leo Peysakhovich
Leo Peysakhovich
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 350
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/column



randy sqlcentral
randy sqlcentral
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5

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
Baklabob
SSC Veteran
SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)

Group: General Forum Members
Points: 282 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?





Michael Irwin
Michael Irwin
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 85
<>

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
Leo Peysakhovich
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 350
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
Leo Peysakhovich
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 350
I didn't wrote the header. Sorry. You are right that there is no user defined function in SQL Server 7.



Leo Peysakhovich
Leo Peysakhovich
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 350

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
Denis Oleinik
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 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)
Leo Peysakhovich
Leo Peysakhovich
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 350

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
Jean Chevalier
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 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




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search