How to use the MySQL CHAR() function

The CHAR() function in MySQL returns the character corresponding to an ASCII code value. It is useful for converting ASCII codes to their character representation.

Posted on

The CHAR() function in MySQL returns the character corresponding to an ASCII code value. It is useful for converting ASCII codes to their character representation.

Syntax

The syntax for CHAR() is:

CHAR(ascii_code)

Where ascii_code is the ASCII code value to convert to a character.

Examples

  1. Convert ASCII code 65 to its character:

    SELECT CHAR(65);
    

    This returns ‘A’, since 65 is the ASCII code for uppercase ‘A’.

  2. Get the character for ASCII code 113:

    SELECT CHAR(113);
    

    This returns ‘q’, since 113 is the code for lowercase ‘q’.

  3. Convert ASCII code 36 to its symbol:

    SELECT CHAR(36);
    

    This returns ‘$’, the dollar symbol represented by ASCII code 36.

  4. Convert a numeric ASCII code to a character:

    SELECT CHAR(109);
    

    This returns ’m’, the character for ASCII code 109.

  5. Get the character of ASCII code 90:

    SELECT CHAR(90);
    

    This returns ‘Z’, since 90 is the ASCII code for uppercase ‘Z’.

Other Similar Functions

Other ASCII-related functions in MySQL:

  • ASCII() - Return ASCII code for a character
  • ORD() - Same as ASCII()
  • CONCAT() - Concatenate strings
  • INSERT() - Insert substring into string

So CHAR() provides a simple way to convert ASCII codes to their corresponding alphanumeric or special characters in MySQL.