Created
November 14, 2012 11:38
-
-
Save wave2/4071667 to your computer and use it in GitHub Desktop.
MSSQL 2000 - Convert varchar hex value to integer (need to remove 0x prefix before calling)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION fn_HexToInt( @HexValue VARCHAR(20) ) | |
RETURNS INTEGER | |
AS | |
BEGIN | |
DECLARE @iOutput INTEGER | |
DECLARE @sChar CHAR(1) | |
DECLARE @iChar INT | |
SET @iOutput = 0 | |
SET @iChar = 0 | |
WHILE @iChar < LEN(@HexValue) | |
BEGIN | |
SET @sChar = SUBSTRING(@HexValue,(LEN(@HexValue) - @iChar),1) | |
SET @iOutput = @iOutput + (POWER(16,(@iChar)) * ( | |
CASE | |
WHEN ISNUMERIC(@sChar) = 1 THEN CONVERT(INT,@sChar) | |
WHEN @sChar = 'A' THEN 10 | |
WHEN @sChar = 'B' THEN 11 | |
WHEN @sChar = 'C' THEN 12 | |
WHEN @sChar = 'D' THEN 13 | |
WHEN @sChar = 'E' THEN 14 | |
WHEN @sChar = 'F' THEN 15 | |
END | |
)) | |
-- Chop off the digit we just processed | |
SET @iChar = @iChar + 1 | |
END | |
RETURN @iOutput | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment