- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements