MySQL ORD() Function
The MySQL ORD() function returns the character code for leftmost character of the argument.
If the leftmost character of the argument is a multibyte character, this function returns the code for that character, calculated from the numeric values of its constituent bytes using formula given below:
(1st byte code) + (2nd byte code * 256) + (3rd byte code * 256^2) ...
If the leftmost character of the argument is not a multibyte character, this function returns the same value as the ASCII() function.
Syntax
ORD(string)
Parameters
string |
Required. Specify the string whose leftmost character code is to be find. |
Return Value
Returns the code of the leftmost character in a string.
Example 1:
The example below shows the usage of ORD() function.
mysql> SELECT ORD('A'); Result: 65 mysql> SELECT ORD(1); Result: 49 mysql> SELECT ORD('AlphaCodingSkills.com'); Result: 65 mysql> SELECT ORD(123); Result: 49 mysql> SELECT ORD('@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 ORD() function is used to get the character code for leftmost character of the Name column value.
SELECT *, ORD(Name) AS ORD_Val FROM Employee;
This will produce the result as shown below:
EmpID | Name | City | ORD_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