Re: convert from hexadecimal to a decimal

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 09/29/04


Date: Wed, 29 Sep 2004 08:48:50 +0200

On Wed, 29 Sep 2004 00:27:03 -0400, Steve Kass wrote:

>This is not too efficient, but it should do the trick.

Hi Steve,

How about using a numbers table to speed it up?

create function hexchar2(
  @b varchar(10)
) returns int
as begin
  declare @n bigint
  set @b = substring(@b,3,8)
  set @n = (select
sum((charindex(left(right(@b,n),1),'0123456789ABCDEF')-1)*POWER(16,(n-1)))
            from dbo.numbers
            where n between 1 and len(@b))
  return
    case when @n >= 0X80000000
         then @n - 0x0100000000
    else @n end
end
go

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages