What’s a short/simplest way of formatting an int to an hexa, with leading zeroes? For example I need to format:
x
| int | hexa |
|-------|------|
| 0 | 0000 |
| 1 | 0001 |
| 3861 | 0f15 |
| 14908 | 3a3c |
My query needs many of these conversions I would appreciate a short piece of code instead of a verbose one. Just need to deal with positive numbers.
Advertisement
Answer
I’m not sure it’s the shortest way but at least I found one:
LPAD(TO_HEX(N), 4, '0')
as in:
WITH DATA AS (
SELECT 0 AS N
UNION ALL SELECT 1
UNION ALL SELECT 3861
UNION ALL SELECT 14908
)
SELECT LPAD(TO_HEX(N), 4, '0') FROM DATA