SQL ASCII Function
The SQL ASCII function returns the ASCII (American Standard Code for Information Interchange) code for the first character of a given string.
ASCII codes are numerical representations of characters, commonly used in computing.
SQL ASCII function is useful when you need to know the ASCII code of specific characters or compare characters based on their ASCII values.
In this tutorial, we will go through SQL ASCII String function, its syntax, and how to use this function in SQL statements, with the help of well detailed examples.
Syntax of SQL ASCII Function
The basic syntax of the SQL ASCII function is:
SELECT ASCII('character_string') AS ascii_code;
Each part of this syntax has a specific purpose:
- ASCII(‘character_string’): Returns the ASCII code of the first character in the specified string.
- AS ascii_code: Assigns an alias to the result for easier reference in output.
Step-by-Step Examples with MySQL
We’ll go through various examples demonstrating the ASCII function in MySQL. Using MySQL 8.0 with MySQL Workbench, we will apply the ASCII function to different strings and evaluate their ASCII values.
Example 1: Getting the ASCII Code for a Single Character
To get the ASCII code for the letter “A”:
SELECT ASCII('A') AS ascii_code;
This query returns 65, which is the ASCII code for the uppercase letter “A”.
