SQL RAWTOHEX function
Convert between RAW bytes and hex text. Applied to a string, it exposes the stored encoding byte by byte.
- Categoría
- Strings
- Returns
string / raw- In this category
- 55 of 172
Sintaxis
RAWTOHEX(raw)
HEXTORAW(hex)
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Sí | Supported under this name. |
Ejemplos
Oracle
SELECT RAWTOHEX('ab') AS h FROM dual; -- 6162
Oracle
SELECT RAWTOHEX('café') AS h FROM dual; -- 636166C3A9
Conviene saberlo
- On a VARCHAR2, RAWTOHEX implicitly casts to RAW first, so you see the database charset's bytes: RAWTOHEX('café') shows the UTF-8 é as C3A9.
- HEXTORAW is the inverse, returning RAW. Compare and store it as binary, not text.
- Values above 2000 bytes hit RAW limits: for BLOBs, DBMS_LOB-level routines or UTL_RAW on chunks are the route.
- Other engines: HEX/UNHEX on MySQL, hex() on SQLite, encode/decode on PostgreSQL.
Questions about RAWTOHEX / HEXTORAW
Why does RAWTOHEX('café') give 5 bytes for 4 characters?
It hexes the stored bytes, and AL32UTF8 encodes é as two bytes (C3A9). Four characters, five bytes.
How is this different from MySQL's HEX?
Same concept; Oracle's function is typed: it converts through RAW, so non-binary input is cast first. MySQL's HEX accepts strings and numbers directly.
How do I turn hex back into a string?
HEXTORAW gives RAW; for readable text, UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW(hex)) under the right charset.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Binary and blob hex literals in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Generate a UUID in SQL in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Compute MD5 or SHA-256 hashes of values in SQL Snippet
Cheat sheets for the engines that have it: Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.