Skip to main content
Version: V1.0.0

CHAR

Declaration

CHAR(value1,... [USING charset_name])

Description

Converts each parameter to an integer and returns a string composed of characters corresponding to the code values of these integers, skipping NULL values.

Additionally, if a parameter exceeds 255, it will be converted into multiple result bytes. For example, CHAR(256) is equivalent to CHAR(1,0), and CHAR(256*256) is equivalent to CHAR(1,0,0).

By default, the character set of the string returned by CHAR() is binary. You can specify the character set using the USING clause.

 SELECT CHARSET(char('')), CHARSET(char('' USING utf8mb4));
+-------------------+---------------------------------+
| CHARSET(char('')) | CHARSET(char('' USING utf8mb4)) |
+-------------------+---------------------------------+
| binary | utf8mb4 |
+-------------------+---------------------------------+
1 row in set (0.001 sec)

If the returned value is invalid in the specified character set, a WARNING is thrown. Specifically, if sql_mode is set to STRICT_ALL_TABLES or STRICT_TRANS_TABLES, and the returned value is invalid in the specified character set, NULL is returned.

For more information about sql_mode, see sql_mode.

SET SESSION sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.000 sec)
SELECT CHAR(399 USING utf8);
+----------------------+
| CHAR(399 USING utf8) |
+----------------------+
| NULL |
+----------------------+
1 row in set, 1 warning (0.001 sec)

Examples

SELECT CHAR(79,99,101,97,110,66,97.4,115,'101');
+------------------------------------------+
| CHAR(79,99,101,97,110,66,97.4,115,'101') |
+------------------------------------------+
| OceanBase |
+------------------------------------------+
1 row in set (0.001 sec)