SQL SERVER – Answer – How to Convert Hex to Decimal or INT

SQL SERVER - Answer - How to Convert Hex to Decimal or INT Hexadecimal-Clock Has it ever happened to you that you say something but forget to follow up due to any reason? It usually does not happen to me as I try to remember everything in my task list but there is always an exception. Last year I asked a question regarding about how to convert Hex to Decimal. I promised that I will post an answer with Due Credit to the author but never got around to post a blog post around it. Read the original post over here SQL SERVER – Question – How to Convert Hex to Decimal.

The matter of the fact, I received excellent answers to my earlier questions and the blog post has been excellent resource of the solution when it someone wants to convert Hex to Decimal.

The hex value of int 256 is 0x00000100. We will use the same for all of our examples.

Let us go over some of the contributions and solutions:

1) Solution by Fly

Conversion from INT to HEX

Solarwinds
SELECT CONVERT(VARBINARY(8), 256)

Converting from HEX to INT

SELECT CONVERT(INT, 0x00000100)

2) Solution by Madhivanan

Madhivanan has written very interesting note regarding how Hex can be converted to Int.

SELECT 0x00000100*1 HextoInt

You just have to multiply Hex with *1 and it will convert it to Int.

He has written a follow up blog post on the same subject.

3) Solution by Ray and knightEknight

DECLARE @hex VARCHAR(64) = '0x00000100'
SELECT CAST( CONVERT(VARBINARY,'0x'+RIGHT('00000000'+REPLACE(@hex,'x',''),8),1) AS INT)

4) Solution by SPV

SELECT CONVERT(INT, CONVERT(VARBINARY, '0x00000100', 1))

Another Remarkable Solutions

Feodor has excellent solution where he has created a function which can do this task.

Marko Parkkola has come up with an excellent solution with CLR.

James Curran and Vedran Kesegic have excellent solution which very interesting as well.

Well, it was an interesting experience and I had really fun experimenting with all the solutions.

Which one is your favorite solution out of all the above solutions?

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
,
Previous Post
SQL SERVER – How do I Record Video and Webcast – Milestone – 2200th Blog Post – SQL in Sixty Seconds #022 – Video
Next Post
SQL SERVER – Query to Get Unique Distinct Data Based on Condition – Eliminate Duplicate Data from Resultset

Related Posts

9 Comments. Leave new

Leave a Reply

Menu