In MariaDB, CHAR()
is a built-in string function that returns characters based on their code values.
CHAR()
accepts one or more integers. It then returns a string consisting of the characters given by the code values of those integers.
Syntax
The syntax goes like this:
CHAR(N,... [USING charset_name])
Where N,...
is one or more values that CHAR()
interprets as int
values.
The optional USING
argument can be used to specify the character set to use.
Example
Here’s a simple example to demonstrate:
SELECT CHAR(77);
Result:
+----------+ | CHAR(77) | +----------+ | M | +----------+
Here I provided just one integer, and so CHAR()
returned just one character. In this case, the integer 77
maps to the uppercase letter M
.
Multiple Characters
We can provide multiple integers, separated by a comma, which will result in multiple characters being returned.
Example:
SELECT CHAR(77, 97, 114, 105, 97);
Result:
+----------------------------+ | CHAR(77, 97, 114, 105, 97) | +----------------------------+ | Maria | +----------------------------+
The USING
Argument
We can use the optional USING
argument to specify a character set to use.
Example:
SELECT CHAR(49833 USING utf8);
Result:
+------------------------+ | CHAR(49833 USING utf8) | +------------------------+ | © | +------------------------+
Case Sensitivity
Uppercase letters have a different value to their lowercase equivalents. Therefore, a different integer is required for each.
Example:
SELECT
CHAR(72),
CHAR(104);
Result:
+----------+-----------+ | CHAR(72) | CHAR(104) | +----------+-----------+ | H | h | +----------+-----------+
Null Values
null
arguments are skipped.
Example:
SELECT CHAR(77, null, 77);
Result:
+--------------------+ | CHAR(77, null, 77) | +--------------------+ | MM | +--------------------+
If null
is the only argument, then nothing is returned:
SELECT CHAR(null);
Result:
+------------+ | CHAR(null) | +------------+ | | +------------+