MySQL ASCII Function

MySQL ASCII function is one of the String Functions, which returns the ASCII code of the leftmost character of a character expression. The basic syntax of this is as shown below:

SELECT ASCII (Expression)
FROM [Source]

Expression: Please specify a valid Expression for which you want to find. If the specified expression is more than one character, then the MySQL function returns the ASCII code of the leftmost character of the expression.

To demonstrate this, We are going to use the customerdetails table data that we have shown below.

Customer Table

MySQL ASCII Function Example

This Function returns the ASCII code of the leftmost character in an expression. The following query shows multiple ways to use this function.

TIP: Please refer to the Table to check the values for each MySQL character.

SELECT ASCII('A');

SELECT ASCII(1);

SELECT ASCII('abcd');

SELECT ASCII('');

SELECT ASCII(NULL);
MySQL ASCII Function Example 1

In this example, we are going to implement it on different columns in a table. The following String method statement returns the code of the left most letter in the First_Name, Last_Name, and Yearly_Income columns.

USE company;
SELECT  First_Name, 
		ASCII(First_Name) AS ASCII_FName,
		Last_Name,
        ASCII(Last_Name) AS ASCII_LName,
        Education, 
        Profession, 
        Yearly_Income, 
        ASCII(Yearly_Income) AS ASCII_Income,        
        Sales
FROM customerdetails;

From the below screenshot, you can observe that 84 = T and 57 means 9.

MySQL ASCII Function Example 2