I have a table that has a TEXT
column which contains hexadecimal numbers.
I now need to represent them as the integers that they really are, but found no
way of doing that (something like to_hex()
in reverse).
I am aware that I can convert literal hexadecimal values like this:
SELECT x'DEADBEEF';
But how do I apply something like this if the value to be converted comes from a
column? Concatenating 'x'
to the column name obviously doesn't work, because then
it is no longer a string literal.
I found a very ugly function in the PostgreSQL mailing lists which pieces together a query string such that the function argument is then again a literal, and then executes that function, but the approach is just downright perverse---there has to be a better way. At least I hope so, given that the message is almost ten years old…
Of course, I know that having the value in question stored as an integer in the database in the first place would be the way to go. But this is not possible in this case, so I'm stuck with trying to decypher those strings…)