PostgreSQL ascii() Function

The PostgreSQL ascii() function returns the numeric code of the first character of the specified string.

ascii() Syntax

This is the syntax of the PostgreSQL ascii() function:

ascii(text)

Parameters

text

Required. The character whose numeric code to return. If there is more than one character, ascii() only returns the ascii value of the first character of text.

Return value

The PostgreSQL ascii() function returns the numeric code of the first character of the specified string.

If the encoding of the specified string is UTF8, this function returns the Unicode code point of the first character. Otherwise it returns the ascii code of the first character.

ascii() Examples

If you want to return the ascii value of the first character of a given text, use the following statement:

SELECT
    ascii('h') AS "ascii('h')",
    ascii('hello') AS "ascii('hello')";
  h  | hello
-----+-------
 104 |   104

You can also return the Unicode code point of a character encoded in UTF8.

SELECT
    ascii('你') AS "ascii('你')",
    ascii('你好') AS "ascii('你好')";
 ascii('你') | ascii('你好')
-------------+---------------
       20320 |         20320