MySQL ASCII() Function
The MySQL ASCII() function returns the ASCII value of specified character. If a string is entered, the function returns the ASCII value for the first character of the string only.
Syntax
ASCII(character)
Parameters
character |
Required. Specify the character to return the ASCII value for. If more than one character is entered, the function will only return the value for the first character. |
Return Value
Returns the ASCII value of a character.
Example 1:
The example below shows the usage of ASCII() function.
mysql> SELECT ASCII('A'); Result: 65 mysql> SELECT ASCII(1); Result: 49 mysql> SELECT ASCII('AlphaCodingSkills.com'); Result: 65 mysql> SELECT ASCII(123); Result: 49 mysql> SELECT ASCII('@gmail.com'); Result: 64
Example 2:
Consider a database table called Employee with the following records:
EmpID | Name | City |
---|---|---|
1 | John | London |
2 | Marry | New York |
3 | Jo | Paris |
4 | Kim | Amsterdam |
5 | Ramesh | New Delhi |
6 | Huang | Beijing |
In the query below, the ASCII() function is used to get the ASCII value of first character of Name column value of Employee table.
SELECT *, ASCII(Name) AS ASCII_Val FROM Employee;
This will produce the result as shown below:
EmpID | Name | City | ASCII_Val |
---|---|---|---|
1 | John | London | 74 |
2 | Marry | New York | 77 |
3 | Jo | Paris | 74 |
4 | Kim | Amsterdam | 75 |
5 | Ramesh | New Delhi | 82 |
6 | Huang | Beijing | 72 |
❮ MySQL Functions