MariaDB CHAR() Function

In MariaDB, CHAR() is a built-in string function that converts each integer argument to the corresponding character and returns a string consisting of those characters.

MariaDB CHAR() Syntax

Here is the syntax of the MariaDB CHAR() function:

CHAR(num1, [num2, ...] [USING charset])

Parameters

num1, [num2, ...]

Required. Some integer, or a value that can be converted to an integer.

USING charset

Optional. It specifies the character set used by the returned string.

Return value

The MariaDB CHAR() function returns a string consisting of the characters corresponding to each integer argument.

The value null values in the parameter num1, [num2, ...] will be ignored by the CHAR() function.

If you only provide a NULL value, the CHAR() function will return an empty string.

The MariaDB CHAR() function returns a binary string by default unless you specify the character set to be returned by USING clause.

MariaDB CHAR() Examples

Single parameter

This statement uses MariaDB CHAR() function to return the character corresponding to the number 65:

SELECT CHAR(65);

Output:

+----------+
| CHAR(65) |
+----------+
| A        |
+----------+

Multiple parameters

The MariaDB CHAR() function supports multiple integer parameters, and combines the characters corresponding to all integers into a string.

This statement uses the MariaDB CHAR() function to return a string consisting of characters corresponding to 72, 101, 108, 108, 111:

SELECT
  CHAR(72) AS "72",
  CHAR(101) AS "101",
  CHAR(108) AS "108",
  CHAR(111) AS "111",
  CHAR(72, 101, 108, 108, 111) AS "ALL";

Output:

+------+------+------+------+-------+
| 72   | 101  | 108  | 111  | ALL   |
+------+------+------+------+-------+
| H    | e    | l    | o    | Hello |
+------+------+------+------+-------+

Use character set

By default, the MariaDB CHAR() function returns a binary string, and you can specify the character set of the returned strings via USING charset.

SELECT CHAR(65 USING utf8mb4);

Output:

+------------------------+
| CHAR(65 USING utf8mb4) |
+------------------------+
| A                      |
+------------------------+

NULL parameters

The MariaDB CHAR() function ignores NULL parameters:

SELECT
  CHAR(65, NULL, 66),
  CHAR(NULL);

Output:

+--------------------+------------+
| CHAR(65, NULL, 66) | CHAR(NULL) |
+--------------------+------------+
| AB                 |            |
+--------------------+------------+

In this example, since there is only one NULL parameter in CHAR(NULL), nothing is returned.

Other Examples

SELECT
    CHAR(65, 66, 67),
    CHAR(65, '66', '67'),
    CHAR(65.4, '66.5', '67.6'),
    CHAR(65, 66, NULL, 67)\G

Output:

          CHAR(65, 66, 67): ABC
      CHAR(65, '66', '67'): ABC
CHAR(65.4, '66.5', '67.6'): ABC
    CHAR(65, 66, NULL, 67): ABC

Conclusion

The MariaDB CHAR() function returns a string consisting of the characters corresponding to all integer arguments.