ALTER FUNCTION dbo.HexToDecimal ( @HexValue varchar(18) ) RETURNS BIGINT AS BEGIN DECLARE @DigitPlace AS INT DECLARE @DecimalValue AS BIGINT DECLARE @PlaceValue as BIGINT DECLARE @PlaceAscii as BIGINT DECLARE @HexDigit as CHAR(1) SET @HexValue = UPPER(@HexValue) IF SUBSTRING(@HexValue,1,2) = '0X' BEGIN SET @HexValue = SUBSTRING(@HexValue,3, LEN(@HexValue) - 2) END SET @DigitPlace = 0 SET @DecimalValue = 0 WHILE @DigitPlace < LEN(@HexValue) BEGIN SET @HexDigit = SUBSTRING(@HexValue, LEN(@HexValue) - @DigitPlace, 1) SET @PlaceAscii = ASCII(@HexDigit) IF @PlaceAscii > 64 BEGIN SET @PlaceValue = (@PlaceAscii - 55) END ELSE BEGIN SET @PlaceValue = (@PlaceAscii - 48) END IF @DigitPlace = 0 BEGIN SET @DecimalValue = @PlaceValue END ELSE BEGIN SET @DecimalValue = @DecimalValue + (@PlaceValue * POWER(16, @DigitPlace)) END SET @DigitPlace = @DigitPlace + 1 END RETURN @DecimalValue END