MariaDB ORD() Function
In MariaDB, ORD()
is a built-in string function that returns the numeric code of the first character in a string argument.
The ORD()
function is an enhanced version of the ASCII()
function, it can handle not only single-byte characters, but also multi-byte characters.
MariaDB ORD()
Syntax
Here is the syntax of the MariaDB ORD()
function:
ORD(string)
Parameters
string
-
Required. a string.
Return value
The MariaDB ORD(string)
function obtains the first character of string
, and then returns the result according to the following rules:
-
If the first character is a single-byte character, the
ORD()
function returns the ASCII value of the character -
If the first character is a multibyte character, the result is calculated according to the following formula:
(1st byte code) + (2nd byte code x 256) + (3rd byte code x 256 x 256) ...
If the argument is an empty string ''
, the ORD()
function will return 0
.
If the argument is NULL
, the ORD()
function will return NULL
.
MariaDB ORD()
Examples
Single-byte character
For single-byte characters, the MariaDB ORD()
function returns their ASCII value, the following statement demonstrates this:
SELECT ORD('A'), ORD('AB');
Output:
+----------+-----------+
| ORD('A') | ORD('AB') |
+----------+-----------+
| 65 | 65 |
+----------+-----------+
In this example, both ORD('A')
and ORD('AB')
return the ASCII value of A
, which is 65, because ORD()
returns only the numeric code for the first character.
Multibyte character
The MariaDB ORD()
function can handle multi-byte characters, as follows:
SELECT ORD('©'), ORD('⟺'), ORD('你'), ORD('你好');
Output:
+-----------+------------+------------+---------------+
| ORD('©') | ORD('⟺') | ORD('你') | ORD('你好') |
+-----------+------------+------------+---------------+
| 49833 | 14852026 | 14990752 | 14990752 |
+-----------+------------+------------+---------------+
Empty string and NULL
If the argument is an empty string ''
, the ORD()
function will return 0
. If the argument is NULL
, the ORD()
function will return NULL
.
SELECT ORD(''), ORD(NULL);
Output:
+---------+-----------+
| ORD('') | ORD(NULL) |
+---------+-----------+
| 0 | NULL |
+---------+-----------+
Conclusion
In MariaDB, ORD()
is a built-in string function that returns the numeric code of the first character in a string argument.