SQL Tutorial SQL Advanced SQL Database SQL References

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:

EmpIDNameCity
1JohnLondon
2MarryNew York
3JoParis
4KimAmsterdam
5RameshNew Delhi
6HuangBeijing

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:

EmpIDNameCityORD_Val
1JohnLondon74
2MarryNew York77
3JoParis74
4KimAmsterdam75
5RameshNew Delhi82
6HuangBeijing72

❮ MySQL Functions