// strings reference

SQL ASCII function

The inverse of CHR/CHAR: text in, integer code out. Only the first character is read, and encoding decides what multibyte input returns.

Categoría
Strings
Returns
integer
In this category
51 of 172

Sintaxis

ASCII(string)
ORD(string)
unicode(string)

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

Soporte del motor

MotorSupportNotas
SQL Server Sí Supported under this name.
PostgreSQL Sí Supported under this name.
MySQL Sí Supported under this name.
SQLite Different name or syntax no ASCII(); unicode(X) returns the code point
Oracle Sí multibyte characters return their encoded bytes as one number: ASCII('é') is 50089

Ejemplos

MySQL

SELECT ASCII('A') AS code;  -- 65

MySQL

SELECT ASCII('é') AS first_byte, ORD('é') AS ord;
-- 195, 50089

SQLite

SELECT unicode('é') AS cp;  -- 233

Conviene saberlo

  • Only the first character counts: ASCII('Banana') is 66. An empty string gives 0 on MySQL and PostgreSQL but NULL on SQL Server, and NULL on Oracle because '' is NULL there.
  • Multibyte input splits the engines: MySQL's ASCII('é') returns the first UTF-8 byte (195); MySQL's ORD() and Oracle's ASCII() return both bytes as one number (0xC3A9 = 50089); PostgreSQL and SQL Server's ASCII return 233.
  • SQLite has no ASCII(); unicode(X) returns the full Unicode code point (unicode('é') is 233).
  • SQL Server's ASCII works on varchar codepages; for nvarchar, UNICODE() is the matching function.

Questions about ASCII

Why is ASCII('é') not 233?

On MySQL it reads the first stored byte, and UTF-8 encodes é as two bytes (195, 169). ORD() and Oracle's ASCII give 50089, the two bytes read as one number. PostgreSQL's ascii() and SQLite's unicode() give the code point 233.

What does ASCII('') return?

0 on MySQL and PostgreSQL; NULL on SQL Server, and on Oracle, whose empty string is NULL. Check the length first when empty strings are possible.

How do I go from code back to character?

CHR(n) on PostgreSQL and Oracle, CHAR(n) on SQL Server, MySQL and SQLite, the exact inverse for the same code space.

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