Oracle ASCII() Function

Oracle ASCII() is a built-in function that returns the decimal numeric representation of the first character in the string specified by the parameter. The return value of the Oracle ASCII() function is related to the character set of the database.

Oracle ASCII() syntax

Here is the syntax for the Oracle ASCII() function:

ASCII(character)

Parameters

character

Required. The character whose ASCII value is to be returned. If there is more than one character, it will just return the ASCII value of the first character. It can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2 data type.

Return Value

The Oracle ASCII() function returns the decimal numeric representation of one of the given characters. The type of the return value is NUMBER.

If the character parameter is NULL or an empty string, it will return NULL.

Oracle ASCII() Examples

Here are some examples that demonstrate the usage of the Oracle ASCII() function.

Basic example

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

SELECT
    ASCII('h') AS "ASCII('h')",
    ASCII('hello') AS "ASCII('hello')"
FROM dual;
 ASCII('h')   ASCII('hello')
------------ ----------------
        104              104

Case Sensitive

The ASCII value of uppercase letters is different from that of lowercase letters.

SELECT
    ASCII('A') AS "A",
    ASCII('a') AS "a"
FROM dual;

Output:

    A     a
_____ _____
   65    97

NULL and the empty string

If the parameter is NULL or an empty string, the Oracle ASCII() function returns NULL.

SET NULL 'NULL';
SELECT
    ASCII(NULL),
    ASCII('')
FROM dual;

Output:

   ASCII(NULL)    ASCII('')
______________ ____________
          NULL         NULL

In this example, we use the statement SET NULL 'NULL'; to display NULL values as the string 'NULL'.

Conclusion

Oracle ASCII() is a built-in function that returns the decimal numeric representation of the first character in the string specified by the parameter. The return value of the Oracle ASCII() function is related to the character set of the database.