CHAR( )

This MySQL function returns a string corresponding to the numeric code passed as the argument.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

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

Explanation

This function returns a string corresponding to the numeric code passed as the argument. This is the reverse of ASCII( ). 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).

Examples

As an example of this function's use, suppose that a college database had a table for fraternities on campus and that the table had a column to contain the Greek letters for the 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.