MySQL String ORD Function

The MySQL string ORD function is one of the MySQL String Functions, which is to find the code of the leftmost character in a string.

  • If the leftmost character is multibyte, the ORD function uses the formula to calculate the code: (1st-byte code) + (2nd-byte code *256) + (3rd-byte code * 256^2)….
  • If the leftmost character is not a multibyte character, this MySQL string ORD function writes the. ASCII code of that byte.

MySQL String ORD Syntax

The basic syntax of the ORD string in MySQL is as shown below.

ORD(String)

MySQL ORD Function Example

The following query shows multiple ways to use this MySQL String ORD function. Here, we used only single characters; that’s why this function returns the ASCII values of those characters.

SELECT ORD('A');

SELECT ORD('1');

SELECT ORD('@');
MySQL String ORD Function 1

Within the first two statements, we used multiple values. However, this MySQL function returns the ASCII code value of the first character. In the third statement, we used a different character. So, this String method used the formula to get that code.

SELECT ORD('ABC');

SELECT ORD('213');

SELECT ORD('¥z');
MySQL ORD Function 2