w3resource

MySQL ASCII() function

ASCII() function

MySQL ASCII() returns the ASCII value of the leftmost character of a given string.

This function is useful in -

  • Get ASCII values: It allows you to retrieve the ASCII value of a single character. For example, ASCII('B') will return 66, as 66 is the ASCII value of the uppercase letter 'B'.
  • Sorting: The ASCII() function can be used to sort data based on ASCII values. For instance, we can order a result set based on the ASCII values of characters in a particular column.
  • Character analysis: By using the ASCII() function, you can analyze and manipulate characters based on their ASCII values.

Syntax:

ASCII(str1)

Argument:

Name Description
str1 A string whose ASCII value of the first character is to be retrieved.

The NULL within an ASCII function will return NULL.

The ASCII() will return 0 If the string is empty.

Syntax Diagram:

MySQL ASCII() Function - Syntax Diagram

MySQL Version: 8.0

Example: MySQL ASCII() function

The following MySQL statement will return the ASCII value of b and B.

Code:

SELECT ASCII('b')AS Lower_Case, ASCII('B') AS Upper_Case;

Output:

mysql> SELECT ASCII('b')AS Lower_Case, ASCII('B') AS Upper_Case; 
+------------+------------+
| Lower_Case | Upper_Case |
+------------+------------+
|         98 |         66 | 
+------------+------------+
1 row in set (0.03 sec)			  

Example of MySQL ASCII() function using where clause

The following MySQL statement will return those authors (from author table), whose name's first character's ASCII value is less than seventy.

Code:

SELECT aut_name,ASCII(aut_name)as "ASCII value of 1st character" 
FROM author 
WHERE ASCII(aut_name)<70;

Sample table: author


Output:

mysql> SELECT aut_name,ASCII(aut_name)as "ASCII value of 1st character" 
    -> FROM author 
    -> WHERE ASCII(aut_name)< 70; 
+--------------+------------------------------+
| aut_name     | ASCII value of 1st character |
+--------------+------------------------------+
| Evan Hayek   |                           69 | 
| E. Howard    |                           69 | 
| C. J. Wilde  |                           67 | 
| Butler Andre |                           66 | 
+--------------+------------------------------+
4 rows in set (0.10 sec)			  

Use ASCII() in an ORDER BY clause for custom sorting

The following is a MySQL statement that returns authors (from the author table), in alphabetical order.

Code:

SELECT aut_name 
FROM author 
ORDER BY ASCII(SUBSTRING(aut_name, 1, 1)), aut_name;

Sample table: author


Output:

aut_name            |
--------------------+
Butler Andre        |
C. J. Wilde         |
E. Howard           |
Evan Hayek          |
John Betjeman Hunter|
Joseph Milton       |
Marquis de Ellis    |
Nikolai Dewey       |
Piers Gibson        |
S.B.Swaminathan     |
.....			  

This query will retrieve the names of authors from the "author" table and order them based on the ASCII value of their first character. It can be useful when you want to sort names in a custom order.

Find Non-Ascii characters in MySQL

According to wikipedia "The American Standard Code for Information Interchange (ASCII /ˈæski/ ass-kee) is a character-encoding scheme originally based on the English alphabet that encodes 128 specified characters - the numbers 0-9, the letters a-z and A-Z, some basic punctuation symbols, some control codes that originated with Teletype machines, and a blank space - into the 7-bit binary integers".

To find non ASCII characters from a MySQL table you can use the following query with a regular expression. This regular expression ([A-Za-z0-9.,-]) shows all characters except a-z, A-Z, 0-9, periods, commas, and hyphens. You can modify the regular expression as per your requirement.

SQL Query:

SELECT * FROM table_name WHERE NOT column_to_check REGEXP '[A-Za-z0-9.,-]';

Let see the following MySQL statements .

mysql> CREATE TABLE TEST(test_char varchar(8));
Query OK, 0 rows affected (0.60 sec)
mysql> INSERT INTO test VALUES('abcd');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('æ');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('123xyz');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('É');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO TEST VALUES('--');
Query OK, 1 row affected (0.05 sec)
mysql> SELECT * FROM TEST;

+-----------+
| test_char |
+-----------+
| abcd      |
| æ         |
| 123xyz    |
| É         |
| --        |
+-----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM TEST WHERE NOT test_char REGEXP '[A-Za-z0-9.,-]';

+-----------+
| test_char |
+-----------+
| æ         |
| É         |
+-----------+
2 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: NULLIF
Next: BIN



Follow us on Facebook and Twitter for latest update.