|
| 1 | +CHAR(N,...[USING charset_name]) |
| 2 | + |
| 3 | +<!-- Though build:dialect it not working --> |
| 4 | + |
| 5 | +CHAR() interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers. NULL values are skipped. |
| 6 | + |
| 7 | +``` |
| 8 | +mysql> SELECT CHAR(77,121,83,81,'76'); |
| 9 | ++--------------------------------------------------+ |
| 10 | +| CHAR(77,121,83,81,'76') | |
| 11 | ++--------------------------------------------------+ |
| 12 | +| 0x4D7953514C | |
| 13 | ++--------------------------------------------------+ |
| 14 | +1 row in set (0.00 sec) |
| 15 | +
|
| 16 | +mysql> SELECT CHAR(77,77.3,'77.3'); |
| 17 | ++--------------------------------------------+ |
| 18 | +| CHAR(77,77.3,'77.3') | |
| 19 | ++--------------------------------------------+ |
| 20 | +| 0x4D4D4D | |
| 21 | ++--------------------------------------------+ |
| 22 | +1 row in set (0.00 sec) |
| 23 | +``` |
| 24 | + |
| 25 | +By default, CHAR() returns a binary string. To produce a string in a given character set, use the optional USING clause: |
| 26 | + |
| 27 | +``` |
| 28 | +mysql> SELECT CHAR(77,121,83,81,'76' USING utf8mb4); |
| 29 | ++---------------------------------------+ |
| 30 | +| CHAR(77,121,83,81,'76' USING utf8mb4) | |
| 31 | ++---------------------------------------+ |
| 32 | +| MySQL | |
| 33 | ++---------------------------------------+ |
| 34 | +1 row in set (0.00 sec) |
| 35 | +
|
| 36 | +mysql> SELECT CHAR(77,77.3,'77.3' USING utf8mb4); |
| 37 | ++------------------------------------+ |
| 38 | +| CHAR(77,77.3,'77.3' USING utf8mb4) | |
| 39 | ++------------------------------------+ |
| 40 | +| MMM | |
| 41 | ++------------------------------------+ |
| 42 | +1 row in set, 1 warning (0.00 sec) |
| 43 | +
|
| 44 | +mysql> SHOW WARNINGS; |
| 45 | ++---------+------+-------------------------------------------+ |
| 46 | +| Level | Code | Message | |
| 47 | ++---------+------+-------------------------------------------+ |
| 48 | +| Warning | 1292 | Truncated incorrect INTEGER value: '77.3' | |
| 49 | ++---------+------+-------------------------------------------+ |
| 50 | +1 row in set (0.00 sec) |
| 51 | +``` |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +If CHAR() is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”. |
| 56 | + |
| 57 | +CHAR() 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): |
| 58 | + |
| 59 | +``` |
| 60 | +mysql> SELECT HEX(CHAR(1,0)), HEX(CHAR(256)); |
| 61 | ++----------------+----------------+ |
| 62 | +| HEX(CHAR(1,0)) | HEX(CHAR(256)) | |
| 63 | ++----------------+----------------+ |
| 64 | +| 0100 | 0100 | |
| 65 | ++----------------+----------------+ |
| 66 | +1 row in set (0.00 sec) |
| 67 | +
|
| 68 | +mysql> SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256)); |
| 69 | ++------------------+--------------------+ |
| 70 | +| HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) | |
| 71 | ++------------------+--------------------+ |
| 72 | +| 010000 | 010000 | |
| 73 | ++------------------+--------------------+ |
| 74 | +1 row in set (0.00 sec) |
| 75 | +``` |
0 commit comments