How to convert integers to hexadecimal, octal, and binary in SQL Server

Views: 1.812
Reading Time: 3 minutes

Hello everybody!
Good evening.

In this post I will demonstrate how to perform conversions between decimal numbers (base 10) to hexadecimal numbers (base 16) and vice versa using simple SQL Server functions to perform these tasks and in the end I will provide a very interesting function that allows you to convert a number in base 10 to another number from any base (base 2 to 99)

How to convert integer to hexadecimal

Using the function below, you can easily convert an integer to a hexadecimal number.

or we can use a simple CONVERT, but that doesn't bring the result with the formatting I'd like:

or even using the fn_varbintohexstr system function:

Examples of use:
SQL Server - Integer for Hexadecimal

How to convert hexadecimal number to integer

Using the function below, you can quickly convert a hexadecimal digit to an integer.

We can also accomplish this task with another simple CONVERT:

Example of use:
SQL Server - Hexadecimal to Integer 2

How to convert binary number to decimal

Using the function below, we can convert binary numbers to decimals:

Usage examples:
SQL Server - Binary to Decimal

How to convert a decimal number to a generic base

Finally, this is the final solution for converting decimal numbers to different bases, because the function allows to perform the conversion to binary, hexadecimal, octal, etc.

Usage examples:
SQL Server - Decimal to Octal Hexadecimal Binary

Thanks for stopping by and see you next time!

How to convert integer to Hexadecimal in SQL Server how to convert binary octal binary number binary

How to convert integer to Hexadecimal in SQL Server how to convert binary octal binary number binary