How the ORD() function works in Mariadb?
The ORD()
function is a built-in function in Mariadb that returns the numeric ASCII code of the first character of a string value.
The ORD()
function is a built-in function in Mariadb that returns the numeric ASCII code of the first character of a string value. The function is useful for converting characters to numbers or vice versa. The function is also known as ASCII()
.
Syntax
The syntax of the ORD()
function is as follows:
ORD(str)
Where str
is a string value. If str
is NULL
or an empty string, the function returns NULL
.
Examples
Example 1: Getting the ASCII code of a single character
The following example shows how to use the ORD()
function to get the ASCII code of a single character:
SELECT ORD('A') AS ASCII;
The output is:
+-------+
| ASCII |
+-------+
| 65 |
+-------+
The function returns 65, which is the ASCII code of the character ‘A’.
Example 2: Getting the ASCII code of the first character of a string
The following example shows how to use the ORD()
function to get the ASCII code of the first character of a string:
SELECT ORD('Hello') AS ASCII;
The output is:
+-------+
| ASCII |
+-------+
| 72 |
+-------+
The function returns 72, which is the ASCII code of the character ‘H’. The function only considers the first character of the string, and ignores the rest.
Example 3: Converting a number to a character using the ORD() function and the CHAR() function
The following example shows how to use the ORD()
function and the CHAR()
function to convert a number to a character. The CHAR()
function returns the character corresponding to the ASCII code of a number.
SELECT ORD(65), CHAR(65);
The output is:
+---------+----------+
| ORD(65) | CHAR(65) |
+---------+----------+
| 54 | A |
+---------+----------+
The function ORD(65)
returns 65, which is the ASCII code of the number 65. The function CHAR(65)
returns ‘A’, which is the character corresponding to the ASCII code 65.
Related Functions
There are some other functions in Mariadb that are related to the ORD()
function. They are:
CHAR()
: This function returns the character corresponding to the ASCII code of a number. The function is the inverse of theORD()
function. For example, the functionCHAR(65)
returns ‘A’, which is the character corresponding to the ASCII code 65.CHAR_LENGTH()
: This function returns the number of characters in a string value. The function is also known asLENGTH()
orCHARACTER_LENGTH()
. The function takes into account the character set and encoding of the string, and may return a different value than theOCTET_LENGTH()
function. For example, the string ‘你好’ has aCHAR_LENGTH()
of 2 and anOCTET_LENGTH()
of 6 in the default character set (utf8).SUBSTRING()
: This function returns a substring of a string value. The function is also known asSUBSTR()
orMID()
. The function allows you to specify the starting position and the length of the substring. For example, the functionSUBSTRING('Hello', 2, 3)
returns ’ell’, which is the substring of ‘Hello’ starting from the second character and having a length of three characters.
Conclusion
The ORD()
function is a useful function in Mariadb that allows you to get the ASCII code of the first character of a string value. The function is helpful for converting characters to numbers or vice versa. You can also use other functions like CHAR()
, CHAR_LENGTH()
, and SUBSTRING()
to manipulate strings in different ways. I hope this article helped you understand how the ORD()
function works in Mariadb.