SQL CHAR Function

The SQL CHAR function is used to convert an ASCII code into its corresponding character.

This function is the opposite of the SQL ASCII function, which retrieves the ASCII code for a character.

The CHAR function is useful when you need to convert ASCII values to characters, such as when dynamically generating characters based on numerical codes.

In this tutorial, we will go through SQL CHAR String function, its syntax, and how to use this function in SQL statements, with the help of well detailed examples.


Syntax of SQL CHAR Function

The basic syntax of the SQL CHAR function is:

</>
Copy
SELECT CHAR(ascii_code) AS character_value;

Each part of this syntax has a specific purpose:

  • CHAR(ascii_code): Converts the ASCII code into its corresponding character.
  • AS character_value: Assigns an alias to the result for easier reference in output.

Examples for SQL CHAR with MySQL

We’ll go through various examples demonstrating the CHAR function in MySQL. Using MySQL 8.0 with MySQL Workbench, we will apply the CHAR function to different ASCII values and evaluate their corresponding characters.


1 Converting an ASCII Code to a Character

To get the character for ASCII code 65:

</>
Copy
SELECT CHAR(65) AS character_value;

This query returns 'A', which is the character for ASCII code 65.