// strings reference

SQL UNICODE function

Returns the Unicode code point of the first character of a string.

カテゴリ
Strings
Returns
integer
In this category
116 of 172

構文

UNICODE(string)

エンジンサポート

エンジンSupportメモ
SQL Server はい Supported under this name.
PostgreSQL Different name or syntax ascii() returns the code point in a UTF8 database.
MySQL Not built in No UNICODE; ORD(CONVERT(c USING utf32)) gives the code point.
SQLite はい Supported under this name.
Oracle Not built in No code-point function; ASCII returns the encoded bytes as a number.

例

SQL Server

SELECT UNICODE(N'A') AS code;  -- 65

SQLite

SELECT unicode(word) AS first_code
FROM words;

知っておくと便利なこと

  • SQL Server returns the full code point for characters above U+FFFF only under a supplementary-character (_SC) or UTF-8 collation. Otherwise UNICODE(N'😀') returns 55357, the first UTF-16 surrogate, instead of 128512.
  • SQLite's unicode() returns the code point of the first character, and NULL for an empty string.
  • NCHAR(n) (SQL Server) and char(n) (SQLite) are the inverses: unicode(char(233)) is 233.

Questions about UNICODE

What is the difference between UNICODE and ASCII?

ASCII is meant for 0 to 127, and beyond that it varies: SQL Server returns the code-page byte (233 for é under Latin1 collations), MySQL the first UTF-8 byte (195), Oracle the whole encoded value (50089). UNICODE returns the code point.

Why does UNICODE return 55357 for my emoji?

The column or literal uses a collation without supplementary-character support, so SQL Server sees two UTF-16 surrogates and reports the first. Apply a _SC collation: UNICODE(col COLLATE Latin1_General_100_CI_AS_SC).

How do I convert a code point back to a character?

NCHAR(233) in SQL Server, char(233) in SQLite, chr(233) in PostgreSQL, NCHR(233) in Oracle, and CHAR(233 USING utf32) in MySQL.

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, SQLite.

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