MySQL ASCII() Function

The ASCII() function in MySQL gets the ASCII (American Standard Code for Information Interchange) value of a character. ASCII is a numerical representation of characters, where each character (like 'A', 'b', '!', etc.) has a unique numerical code.



ASCII(): Definition and Usage

ASCII() is helpful when you need to work with the underlying numerical representation of characters, perhaps for character-by-character comparisons, data validation, or specialized text processing. The function returns the ASCII value for the very first character in the input string; any characters beyond the first one are ignored.

Syntax

Syntax

ASCII(character)
      

Parameter Values

Parameter Description
character The character (or string—only the first character is used) for which you want the ASCII value. This is required.

Example

Getting the ASCII Value of the First Character in a Column

This example shows how to get the ASCII value of the first character of each customer's name from the 'Customers' table (assuming a table named 'Customers' exists with a 'CustomerName' column).

Syntax

SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;
      
Output

NumCodeOfFirstChar
------------------
(The ASCII value of the first character of each CustomerName will be displayed here)
      

**Note:** The example output will vary based on the data in your `Customers` table. Each row will display the ASCII value of the first character of the corresponding `CustomerName`. If `CustomerName` is NULL for a row, the output will be NULL for that row.