CHAR()
CHAR(ascii[, ...] [USINGcharacter_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
for each of the three Greek letters for the fraternity Delta Sigma Pi.
If we had a Greek keyboard, we could just type them. If we used a
chart available online in a graphical browser, we could just copy and
paste them into our mysql client. Using the
CONCAT() function, we put the results of each
together to insert the data into the column in the table.