Name

CHAR()

Synopsis

CHAR(ascii[, ...] [USING character_set])

This function returns a string corresponding to the numeric code passed as the argument. This is the reverse of ASCII(), described earlier in this chapter. You can optionally give the USING parameter to specify a different character set to use in relation to the string given. If you give it a value greater than 255, it assumes the amount over 255 is another character. So, CHAR(256) is equivalent to CHAR(1,0).

As an example of this function’s use, suppose that a college database has a table for fraternities on campus and that the table has a column to contain the Greek letters for each fraternity’s name. To create a table with such a column, we would at a minimum enter something like the following:

CREATE TABLE fraternities (
frat_id INT(11),
greek_id CHAR(10) CHARACTER SET greek);

Notice that for the column greek_id we’re specifying a special character set to be used. This can be different from the character set for other columns and for the table. With this minimal table, we enter the following INSERT statement to add one fraternity and then follow that with a SELECT statement to see the results:

INSERT INTO fraternities
VALUES(101,
   CONCAT(CHAR(196 USING greek),
   CHAR(211 USING greek),
   CHAR(208 USING greek)));

SELECT greek_id
FROM fraternities
WHERE frat_id = 101;

+----------+
| greek_id |
+----------+
|  Δ Σ Π   |
+----------+

Using the CHAR() function and looking at a chart showing the Greek alphabet, we figure out the ASCII number ...

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.