July 24, 2004 at 5:20 pm
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/column
September 7, 2004 at 7:46 am
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
September 7, 2004 at 10:35 am
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?
September 8, 2004 at 12:20 pm
<>
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
September 14, 2004 at 5:58 am
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.
September 14, 2004 at 6:01 am
I didn't wrote the header. Sorry. You are right that there is no user defined function in SQL Server 7.
September 14, 2004 at 6:03 am
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.
September 29, 2004 at 9:36 am
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)
September 29, 2004 at 4:39 pm
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.
February 24, 2005 at 3:00 am
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
February 24, 2005 at 3:03 pm
Cool way. And you got a good idea for the implementation.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply