// strings reference

SQL ASCIISTR function

Turns any string into an ASCII-safe form: regular characters pass through, everything else becomes its UTF-16 code in \XXXX notation.

Category
Strings
Returns
string
In this category
84 of 172

Syntax

ASCIISTR(string)

Engine support

EngineSupportNotes
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 Yes Supported under this name.

Examples

Oracle

SELECT ASCIISTR('café') AS a FROM dual;  -- caf\00E9

Oracle

SELECT UNISTR(ASCIISTR('café')) AS back FROM dual;  -- café

Good to know

  • ASCII characters pass through untouched; non-ASCII become 4-hex UTF-16 escapes: ASCIISTR('café') gives caf\00E9.
  • Supplementary characters escape as surrogate pairs: ASCIISTR of an emoji yields \D83D\DE00, two escapes for one character.
  • UNISTR is the exact inverse: UNISTR(ASCIISTR(s)) round-trips the original string.
  • Oracle-only. Elsewhere: PostgreSQL's encode(convert_to(s, 'utf8'), 'hex'), MySQL's HEX, or the application layer for presentation-safe output.

Questions about ASCIISTR

What is ASCIISTR used for?

ASCII-safe transport and debugging: logs, feeds and file names that mangle non-ASCII survive as \XXXX escapes, and UNISTR restores them later.

Why does an emoji become two escapes?

ASCIISTR emits UTF-16 code units, and an emoji is a surrogate pair: \D83D\DE00. UNISTR understands the pair and reassembles the character.

Is ASCIISTR the same as RAWTOHEX?

No. RAWTOHEX shows the stored bytes; ASCIISTR shows UTF-16 code points in escape notation and passes plain ASCII through readable.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: Oracle.

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