2 Ways to Return the ASCII Code for a Given Character in MariaDB

MariaDB includes two built-in functions that allow us to get the ASCII code from a given character.

These functions are:

Both of these functions do the same thing, except that ORD() can handle multi-byte characters (whereas ASCII() can’t).

Below are examples of each function.

The ASCII() Function

The ASCII() function returns the numeric ASCII value of the leftmost character of its string argument.

Example:

SELECT ASCII('Oceans');

Result:

+-----------------+
| ASCII('Oceans') |
+-----------------+
|              79 |
+-----------------+

Bear in mind that only the leftmost character’s ASCII code is returned. Therefore, doing the following produces the same result:

SELECT ASCII('O');

Result:

+------------+
| ASCII('O') |
+------------+
|         79 |
+------------+

See How ASCII() Works in MariaDB for more examples.

The ORD() Function

The ORD() function works the same way, except that it can handle multi-byte characters.

Here’s the same example from above, but using ORD() instead of ASCII():

SELECT ORD('Oceans');

Result:

+---------------+
| ORD('Oceans') |
+---------------+
|            79 |
+---------------+

And here it is on a multi-byte character:

SELECT ORD('©');

Result:

+-----------+
| ORD('©')  |
+-----------+
|     49833 |
+-----------+

See How ORD() Works in MariaDB for more examples.