MySQL CHAR() Function
The MySQL CHAR() function interprets each passed argument as an integer and returns a string consisting of the characters given by the code values of those integers. It ignores the NULL value.
For arguments larger than 255 are 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, This function returns a binary string. To produce a string in a given character set, the optional USING clause can be used. See the example below. If USING is given and the result string is illegal for the given character set, a warning is issued. Also, if strict SQL mode is enabled, the result from CHAR() becomes NULL.
Syntax
CHAR(N,... [USING charset_name])
Parameters
N,... |
Required. Specify integers whose character values (according to the ASCII table) are to be retrieved. |
USING charset_name |
Optional. Used to produce a string in a given character set. |
Return Value
Returns a string consisting of the characters given by the code values of those integers.
Example 1:
The example below shows the usage of CHAR() function.
mysql> SELECT CHAR(72,69,76,76,79); Result: 'HELLO' mysql> SELECT CHAR(72,69,76,NULL,76,79); Result: 'HELLO' mysql> SELECT CHAR('72',69,'76',76,79); Result: 'HELLO' mysql> SELECT CHAR('72.8',69,'76.3',76,79); Result: 'HELLO' mysql> SELECT CHAR(65,66,67); Result: 'ABC' mysql> SELECT CHAR(65,66,67 USING utf8); Result: 'ABC' mysql> SELECT CHARSET(CHAR(65)), CHARSET(CHAR(65 USING utf8)); Result: 'binary', 'utf8'
Example 2:
Consider a database table called Sample with the following records:
Data | x1 | x2 | x3 |
---|---|---|---|
Data1 | 67 | 117 | 116 |
Data2 | 80 | 117 | 116 |
Data3 | 84 | 111 | 111 |
Data4 | 66 | 111 | 119 |
Data5 | 67 | 79 | 68 |
Data6 | 69 | 110 | 100 |
The statement given below can be used to get the string containing characters given by the code values specified by columns x1, x2 and x3.
SELECT *, CHAR(x1, x2, x3) AS CHAR_String FROM Sample;
The query will produce the following result:
Data | x1 | x2 | x3 | CHAR_String |
---|---|---|---|---|
Data1 | 67 | 117 | 116 | Cut |
Data2 | 80 | 117 | 116 | Put |
Data3 | 84 | 111 | 111 | Too |
Data4 | 66 | 111 | 119 | Bow |
Data5 | 67 | 79 | 68 | COD |
Data6 | 69 | 110 | 100 | End |
❮ MySQL Functions