
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Sort only numbers from alphanumeric string in MySQL?
To sort only numbers from alphanumeric string, use ORDER BY RIGHT(). Let us first create a table −
mysql> create table DemoTable1948 ( StudentCode varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1948 values('121John_567'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1948 values('Adam_101'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1948 values('Bob_563'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1948 values('Sam_346'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1948;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | 121John_567 | | Adam_101 | | Bob_563 | | Sam_346 | +-------------+ 4 rows in set (0.00 sec)
Here is the query to sort only numbers from alphanumeric string in MySQL −
mysql> select * from DemoTable1948 order by RIGHT(StudentCode,3);
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | Adam_101 | | Sam_346 | | Bob_563 | | 121John_567 | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to sort an alphanumeric column in MySQL?
- How to remove all non-alphanumeric characters from a string in MySQL?
- Sort in a string mixed with numbers in MySQL?
- Sort strings in Alphanumeric sequence
- How to sort an alphanumeric column with different lengths in MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- How to do alphanumeric sort in MongoDB?
- PHP program to remove non-alphanumeric characters from string
- MySQL sort string number?
- Generate random alphanumeric string in Swift
- How to sort mixed numeric/alphanumeric array in JavaScript
- How to extract only the numbers from a text field in MySQL?
- Sort character values from a column mixed with character and numbers in MySQL?
- Sort String Array alphabetically by the initial character only in Java
- Alphanumeric Abbreviations of a String in C Program?

Advertisements