How CHR() Works in MariaDB

In MariaDB, CHR() is a built-in string function that returns a character based on the code values provided as an argument.

The CHR() function is similar to the CHAR() function, except that CHR() only accepts a single argument. The CHAR() function, on the other hand, accepts one or more arguments. Also, CHAR() accepts an optional USING clause, whereas CHR() doesn’t.

The CHR() function was introduced in MariaDB 10.3.1 to provide Oracle compatibility.

Syntax

The syntax goes like this:

CHR(N)

Where N is the value that CHR() interprets as an integer. CHR() then returns a VARCHAR(1) string consisting of the character given by the code values of that integer.

The character set and collation of the string are set according to the values of the character_set_database and collation_database system variables.

Example

Here’s a simple example to demonstrate:

SELECT CHR(65);

Result:

+---------+
| CHR(65) |
+---------+
| A       |
+---------+

Here I provided an integer, and CHR() returned the corresponding character. In this case, the integer 65 maps to the uppercase letter A.

Case Sensitivity

Here’s an example that distinguishes an uppercase letter from its lowercase counterpart:

SELECT 
    CHR(72),
    CHR(104);

Result:

+---------+----------+
| CHR(72) | CHR(104) |
+---------+----------+
| H       | h        |
+---------+----------+

Null Values

An argument of null returns NULL.

Example:

SELECT CHR(null);

Result:

+-----------+
| CHR(null) |
+-----------+
| NULL      |
+-----------+

Passing Multiple Arguments

Passing more than one argument results in an error:

SELECT CHR(65, 77);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CHR'

If you need to pass multiple arguments, use CHAR() instead.

Example:

SELECT CHAR(65, 77);

Result:

+--------------+
| CHAR(65, 77) |
+--------------+
| AM           |
+--------------+