I have a table with a VARBINARY column. I need to insert a string such as '4D2AFF' which represents the hex values 0x4D, 0x2A and 0xFF respectively. How do I construct this statement?
+3
A:
You can use UNHEX()
function to convert a hexstring with hex pairs to binary and HEX()
to do the other way round.
I.e.
INSERT INTO tbl (col) VALUES (UNHEX('4D2AFF'))
and
SELECT HEX(col) FROM tbl
BalusC
2010-06-27 02:58:12
Works like a charm. Thank you.
Tim
2010-06-27 03:16:41
You're welcome.
BalusC
2010-06-27 04:16:43
A:
The Hex() and Unhex() functions were already mentioned, but I'd like to weigh in as well on an alternative pattern.
How are you using the strings before and after? If you can avoid coversion until the last possible minute, that is highly preferential. That way you don't risk something going wrong, or forgetting whether or not the object you've extracted from the DB has already been converted or not.
aronchick
2010-06-27 03:02:02