MySQL ASCII function is one of the String Function, which returns the ASCII code of the leftmost character of a character expression. The basic syntax of the ASCII Function in MySQL is as shown below:
SELECT ASCII (Expression) FROM [Source]
Expression: Please specify a valid Expression for which you want to find the ASCII code. If the specified expression is more than one character, then MySQL ASCII returns the ASCII code of the leftmost character of the expression.
TIP: Please refer ASCII Table to check the ASCII values for each character.
To demonstrate this string ASCII function, We are going to use the customerdetails table data that we have shown below
MySQL ASCII Example 1
The ASCII Function in MySQL returns the ASCII code of the leftmost character in an expression. The following query shows multiple ways to use this function.
SELECT ASCII('A'); SELECT ASCII(1); SELECT ASCII('abcd'); SELECT ASCII(''); SELECT ASCII(NULL);
MySQL ASCII Function Example 2
In this example, we are going to implement the string ASCII in MySQL on different columns in a table. The following statement returns the ASCII code of the left most character in First_Name, Last_Name, and Yearly_Income column.
-- MySQL String ASCII Example 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 is the ASCII code of T, and 57 is the ASCII code of 9