RAW and hex literals
Every engine can express bytes as hex text, but each picked its own literal syntax and its own conversion functions. Moving a script between engines is a translation exercise.
- Family
- Binary & blobs
- Missing in
- None of the five
- Gotchas
- 5 listed
Engine by engine
SQL Server and MySQL use 0x hex literals, SQLite uses X'...', PostgreSQL outputs bytea as \x hex, and Oracle's RAW type converts with HEXTORAW and RAWTOHEX.
| 引擎 | 类型 | 存储 | 备注 |
|---|---|---|---|
| SQL Server |
VARBINARY(n)
|
n + 2 bytes | Hex literals like 0xAB12; CONVERT(varchar(max), value, 1) renders hex text with 0x, style 2 without it. |
| PostgreSQL |
BYTEA
|
n + 1 or 4 bytes | Input and output in hex ('\xAB12') by default since 9.0; decode('ab12', 'hex') and encode(value, 'hex') convert. |
| MySQL |
BINARY(n)
|
n bytes | 0x hex literals; HEX() returns uppercase hex text and UNHEX() parses it back to bytes. |
| SQLite |
BLOB
|
n bytes | X'AB12' blob literals; hex() renders a blob as uppercase hex text and unhex() (3.41+) parses it back. CAST('AB12' AS BLOB) returns the 4 text bytes, not 2. |
| Oracle |
RAW(n)
|
n bytes | HEXTORAW('AB12') builds bytes, RAWTOHEX converts back; RAW input and output are hex by default. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- PostgreSQL changed bytea output from escape format to hex ('\x' prefix) in 9.0; old drivers and parsers that expect escape format break against modern servers.
- SQLite's X'...' literal requires an even number of hex digits and is only valid as a blob literal - X'ABC' is a syntax error, not three bytes.
- MySQL's HEX() doubles the length and returns uppercase text; UNHEX() pads odd-length input with a leading zero (UNHEX('ABC') is 0x0ABC) and returns NULL for non-hex characters.
- SQL Server's CONVERT(varchar, b, 2) truncates at 30 characters because bare varchar means varchar(30): a 32-byte hash comes back as 15 bytes of hex. Write varchar(max) or an explicit length.
- Oracle converts implicitly between RAW and hex strings: inserting 'AB12' into a RAW column stores 2 bytes, and selecting RAW into a VARCHAR2 yields hex text. The other engines need an explicit function.
Common questions
How do I write binary data as a literal?
SQL Server and MySQL: 0xAB12. SQLite: X'AB12'. PostgreSQL: '\xAB12'::bytea or decode('ab12','hex'). Oracle: HEXTORAW('AB12').
How do I convert a BLOB to hex text?
PostgreSQL: encode(b, 'hex'). MySQL: HEX(b). SQLite: hex(b). SQL Server: CONVERT(varchar(max), b, 2). Oracle: RAWTOHEX(b) for RAW, or RAWTOHEX(DBMS_LOB.SUBSTR(b, 2000, 1)) for a BLOB chunk.
Why does my bytea output start with \x?
PostgreSQL 9.0 switched bytea's default output to hex format with a \x prefix. The setting bytea_output controls it, and escape format is still available for old clients.
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.