SQL ASCII

The SQL ASCII returns the ASCII code of the first character in a string.

Syntax

ASCII(string)Code language: SQL (Structured Query Language) (sql)

Arguments

string
The input string that you want to see the ASCII code.

Return Type

A numeric value from 0 to 255.

The ASCII function return 0 if the string is empty. It returns NULL if the string is NULL.

Note that if the input character is out of the range (0,255),  the ASCII function may issue an error depending on the RDMBS’s implementation of the function.

Examples

The following statement returns ASCII codes of the letter A in uppercase and lowercase:

SELECT ASCII('A') upper_a, ASCII('a') lower_a;
Code language: SQL (Structured Query Language) (sql)
 upper_a | lower_a
---------+---------
      65 |      97
(1 row)Code language: SQL (Structured Query Language) (sql)

The following statement returns the ASCII code of the first character in the string, which is the letter A.

SELECT ASCII('ASCII function demo');Code language: SQL (Structured Query Language) (sql)
 ascii
-------
    65
(1 row)Code language: SQL (Structured Query Language) (sql)
Was this tutorial helpful ?