ascii()

A function returning the numeric code of a character

ascii() is a system function returning the numeric code of the first character of the argument.

ascii() was added in PostgreSQL 7.0.

Usage

ascii ( text ) → integer

This function returns the numeric code of the first character of the argument (subsequent characters are ignored). Despite the name, witjh UTF8 encoding it will also return the Unicode code point of the character.

Change history

Examples

Basic execution example for ascii():

postgres=# SELECT ascii('a');
 ascii 
-------
    97
(1 row)

Unicode code points are also returned:

postgres=# SELECT ascii('ä'), ascii('あ');
 ascii | ascii 
-------+-------
   228 | 12354
(1 row)

Anything beyond the first character in a string is ignored:

postgres=# SELECT ascii('abc');
 ascii 
-------
    97
(1 row)

An empty string returns 0:

postgres=# SELECT ascii('');
 ascii 
-------
     0
(1 row)

Categories

Locale and character set handling, String manipulation, System function