MySQL8 ORD() Functions – String Functions

This function helps in getting the numeric value of the leftmost character of a input string.

[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]

If the leftmost character, returned value
is a multibyte character, the returned value is calculated from the numeric values of its constituent bytes.

is not a multibyte character, the return value is its ASCII code (i.e it gives similar value which ASCII() function returns) .
[/table]

Note : If the leftmost character is not a multibyte character, ORD() returns the same value as the ASCII() function.

ORD() : Syntax

ORD( string );


ORD() : Parameter

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , This is input string from which we want the numeric code of the first character from starting.
[/table]


ORD() : Output

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
Number, returns the numeric value of the leftmost character of the input string.
[/table]


ORD() : Available from

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 5.7
[/table]


ORD() Example 1 : Simple example with literal strings.

mysql> SELECT ORD('Tuts');
+-------------+
| ORD('Tuts') |
+-------------+
|          84 |
+-------------+
1 row in set (0.00 sec)

ORD() Example 2 : Simple example with literal strings.

Ascii value for the e.g.1 is same for both input. Both function returns same values.

mysql> SELECT ORD('T'), ASCII('T');
+----------+------------+
| ORD('T') | ASCII('T') |
+----------+------------+
|       84 |         84 |
+----------+------------+
1 row in set (0.00 sec)

ORD() Example 3 : Multibyte Characters

Below example will have multibyte character. So, returned values are calculated from the numeric values of its constituent bytes.

mysql> SELECT ORD('€'), ORD('§');
+------------+-----------+
| ORD('€')   | ORD('§')  |
+------------+-----------+
|   14844588 |     49831 |
+------------+-----------+
1 row in set (0.00 sec)

ORD() Example 4 : NULL Arguments

It will return NULL, if the argument is NULL. Lets see the below example.

mysql> SELECT ORD(NULL);
+-----------+
| ORD(NULL) |
+-----------+
|      NULL |
+-----------+
1 row in set (0.00 sec)

ORD() Example 5 : Uppercase and Lowercase value are different

Uppercase and Lowercase characters have a different numeric value. Hence, it is Case Sensitive function for alphabets. Below example will show this:

mysql> SELECT ORD('A'), ORD('a'), ASCII('A'), ASCII('a');
+----------+----------+------------+------------+
| ORD('A') | ORD('a') | ASCII('A') | ASCII('a') |
+----------+----------+------------+------------+
|       65 |       97 |         65 |         97 |
+----------+----------+------------+------------+
1 row in set (0.00 sec)

See all MySQL String functions MySQL 8 String Functions.


Related articles : ASCII(), OCT(), LENGTH(), OCTET_LENGTH(), BIT_LENGTH(), CHAR() , CHARACTER_LENGTH(), CHAR_LENGTH().


PHP Related articles : CHR(), ORD() , STRLEN(), SUBSTR_COUNT(), COUNT_CHARS(), STRSTR() ,STRCSPN(), PHP STRING FUNCTIONS().


You may also like...