question

viki2015 avatar image
viki2015 asked

Hex to Decimal conversion

Hi, I wanted to convert 000D-6F00-0260-7C59 into decimal so that the output will look like 3781220527799385. Thanks,
sql-server-2008sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Wimbush avatar image
David Wimbush answered
I think hextoint must be a user-defined function on your system, viki2015. It's not available on my system. As far as I can see, you have to convert this string into a binary datatype first and then that converts to an int. I don't think there's any implicit conversion from hex to integer. This does the trick: DECLARE @a VARCHAR(20) = '000D-6F00-0260-7C59'; DECLARE @b VARBINARY(22) = CONVERT(VARBINARY(22), '0x' + REPLACE(@a, '-', ''), 1); SELECT @b; SELECT CONVERT(INT, @b);
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

viki2015 avatar image viki2015 commented ·
You are right. just little change SELECT CONVERT(BIGINT, CONVERT(VARBINARY(22), '0x' + REPLACE('000D-6F00-0260-7C59', '-', ''), 1));
0 Likes 0 ·
viki2015 avatar image
viki2015 answered
DECLARE @a VARCHAR(20) = '000D-6F00-0260-7C59' SELECT db_name.dbo.hex2int(REPLACE(@a, '-', ''))
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.