// strings reference

SQL RAWTOHEX function

Convert between RAW bytes and hex text. Applied to a string, it exposes the stored encoding byte by byte.

類別
Strings
Returns
string / raw
In this category
55 of 172

語法

RAWTOHEX(raw)
HEXTORAW(hex)

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支援

引擎Support備註
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 是 Supported under this name.

範例

Oracle

SELECT RAWTOHEX('ab') AS h FROM dual;  -- 6162

Oracle

SELECT RAWTOHEX('café') AS h FROM dual;  -- 636166C3A9

小知識

  • 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.

// fewer lookups

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.