PostgreSQL TO_HEX() Function
The PostgreSQL TO_HEX() function returns a string containing hexadecimal representation of an integer value.
Syntax
TO_HEX(num)
Parameters
num |
Required. Specify an integer value to convert to a hexadecimal representation. |
Return Value
Returns the hexadecimal representation of an integer value.
Example 1:
The example below shows the usage of TO_HEX() function.
SELECT TO_HEX(20); Result: '14' SELECT TO_HEX(100); Result: '64' SELECT TO_HEX(500); Result: '1f4' SELECT TO_HEX(1000); Result: '3e8'
Example 2:
Consider a database table called Sample with the following records:
Data | x |
---|---|
Data 1 | 10 |
Data 2 | 20 |
Data 3 | 30 |
Data 4 | 40 |
Data 5 | 50 |
The statement given below can be used to get the hexadecimal representation of values of column x.
SELECT *, TO_HEX(x) AS TO_HEX_Value FROM Sample;
This will produce the result as shown below:
Data | x | TO_HEX_Value |
---|---|---|
Data 1 | 10 | a |
Data 2 | 20 | 14 |
Data 3 | 30 | 1e |
Data 4 | 40 | 28 |
Data 5 | 50 | 32 |
❮ PostgreSQL Functions