Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1057687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:54:28+00:00 2026-05-16T17:54:28+00:00

This answer to what looks like the same question: Convert integer to hex and

  • 0

This answer to what looks like the same question:

Convert integer to hex and hex to integer

..does not work for me.

I am not able to go to a HEX string to an integer using MS SQL server 2005 CAST or CONVERT. Am I missing something trivial? I have searched extensively, and the best I can find are long-winded user functions to go from a hex string value to something that looks like a decimal int. Surely there is a simple way to do this directly in a query using built in functions rather than writing a user function?

Thanks

Edit to include examples:

select CONVERT(INT, 0x89)

works as expected, but

select CONVERT(INT, ‘0x’ + substring(msg, 66, 2)) from sometable

gets me:

"Conversion failed when converting the varchar value ‘0x89’ to data type int."

an extra explicit CAST:

select CONVERT(INT, CAST(‘0x89’ AS VARBINARY))

executes, but returns 813185081.

Substituting ‘Int’, ‘Decimal’, etc for ‘Varbinary’ results in an error. In general, strings that appear to be numeric are interpreted as numeric if required, but not in this case, and there does not appear to be a CAST that recognizes HEX. I would like to think there is something simple and obvious and I’ve just missed it.

Microsoft SQL Server Management Studio Express 9.00.3042.00

Microsoft SQL Server 2005 – 9.00.3080.00 (Intel X86) Sep 6 2009 01:43:32 Copyright (c) 1988-2005 Microsoft Corporation Express Edition with Advanced Services on Windows NT 5.1 (Build 2600: Service Pack 3)

To sum up: I want to take a hex string which is a value in a table, and display it as part of a query result as a decimal integer, using only system defined functions, not a UDF.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:54:28+00:00Added an answer on May 16, 2026 at 5:54 pm

    Thanks for giving some more explicit examples. As far as I can tell from the documentation and Googling, this is not possible in MSSQL 2005 without a UDF or other procedural code. In MSSQL 2008 the CONVERT() function’s style parameter now supoprts binary data, so you can do it directly like this:

    select convert(int, convert(varbinary, '0x89', 1))
    

    In previous versions, your choices are:

    • Use a UDF (TSQL or CLR; CLR might actually be easier for this)
    • Wrap the SELECT in a stored procedure (but you’ll probably still have the equivalent of a UDF in it anyway)
    • Convert it in the application front end
    • Upgrade to MSSQL 2008

    If converting the data is only for display purposes, the application might be the easiest solution: data formatting usually belongs there anyway. If you must do it in a query, then a UDF is easiest but the performance may not be great (I know you said you preferred not to use a UDF but it’s not clear why). I’m guessing that upgrading to MSSQL 2008 just for this probably isn’t realistic.

    Finally, FYI the version number you included is the version of Management Studio, not the version number of your server. To get that, query the server itself with select @@version or select serverproperty('ProductVersion').

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An answer to this question has led me to look into using Event Tracing
This looks like a long question because of all the context. There are 2
I've got a string that looks like this: [%{%B%F{blue}%}master %{%F{red}%}*%{%f%k%b%}%{%f%k%b%K{black}%B%F{green}%}] I want to remove
I just saw this question and one of the answers looks really appealing to
UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks
This answer has helped me largely but hasn't fully resolved my issues. I'm using
The answer to this question boils down to two things: 1) Will it be
I am dynamically creating a combobox like this: public Control GenerateList(Question question) { //
In C# I have an intrusive tree structure that looks like this: public abstract
All, This question probably has a very simple answer - something I'm overlooking. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.