// strings reference

SQL UNISTR function

Builds a string from Unicode escapes: UNISTR('caf\00E9') is café. The ASCII-safe way to ship Unicode through channels that mangle non-ASCII.

Category
Strings
Returns
string
In this category
77 of 172

Syntax

UNISTR('caf\00E9')
unistr('\+01F600')  -- 6-hex form: PostgreSQL and SQL Server

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

Engine support

EngineSupportNotes
SQL Server Yes SQL Server 2025 and Azure SQL; varchar input needs a UTF-8 collation, so pass N'...'
PostgreSQL Yes unistr() from PG 14; accepts \XXXX, \+XXXXXX, \uXXXX and \UXXXXXXXX
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 UNISTR('caf\00E9') AS word FROM dual;  -- café

PostgreSQL

SELECT unistr('caf\00E9') AS word;  -- café

SQL Server

-- SQL Server 2025
SELECT UNISTR(N'caf\00E9') AS word;  -- café

Good to know

  • Oracle accepts only 4 hex digits (\00E9); characters above U+FFFF are written as a UTF-16 surrogate pair, \D83D\DE00. PostgreSQL and SQL Server 2025 also take \+01F600 for the code point directly. A literal backslash needs doubling.
  • SQL Server gained UNISTR in SQL Server 2025 (and Azure SQL); a varchar argument raises Msg 9844 unless its collation is UTF-8, so pass N'...'. Older versions build characters with NCHAR(233).
  • MySQL has no Unicode escape syntax in literals; build characters from bytes with an introducer, _utf8mb4 X'C3A9', or with CHAR(0xC3A9 USING utf8mb4).
  • The output is ordinary text: UNISTR is for writing literals, not for transcoding stored data; the result compares and sorts per its column's collation.

Questions about UNISTR

What is the difference between UNISTR and NCHAR?

UNISTR takes a whole string with embedded escapes; NCHAR takes one integer code point. UNISTR('caf\00E9') is one call; the NCHAR version is a three-part concatenation.

How do I write a literal backslash in UNISTR?

Double it: UNISTR('a\\b') on Oracle, PostgreSQL and SQL Server 2025, the same doubling rule as backslash escapes elsewhere.

Why use escapes instead of the real characters?

ASCII-only channels (legacy loaders, source control, some protocols) corrupt non-ASCII. Escapes survive them, then UNISTR restores the text inside the database.

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: SQL Server, PostgreSQL, 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.