
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Order by numeric value from string records separated by numbers like CSE 15, CSE 11, etc.?
Let us first create a table −
mysql> create table DemoTable1969 ( BranchCode varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1969 values('CSE 101'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 6'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 201'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 110'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1969;
This will produce the following output −
+------------+ | BranchCode | +------------+ | CSE 101 | | CSE 11 | | CSE 15 | | CSE 6 | | CSE 201 | | CSE 110 | +------------+ 6 rows in set (0.00 sec)
Here is the query to order by numeric value from string records separated by numbers −
mysql> select * from DemoTable1969 order by CAST(SUBSTRING(BranchCode,LOCATE(' ',BranchCode)+1) AS SIGNED) DESC;
This will produce the following output −
+------------+ | BranchCode | +------------+ | CSE 201 | | CSE 110 | | CSE 101 | | CSE 15 | | CSE 11 | | CSE 6 | +------------+ 6 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL order by string with numbers?
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- Fetch maximum value from a column with values as string numbers like Value440, Value345, etc. in SQL
- How to ORDER BY LIKE in MySQL?
- Summing numbers present in a string separated by spaces using JavaScript
- MySQL search results by month in format 2015-07-01 11:15:30?
- MySQL select order by acts like a string (not a number)?
- MySQL order by from highest to lowest value?
- MySQL ORDER BY with numeric user-defined variable?
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- MySQL query to display records ordered by numeric difference?
- Multiple LIKE Operators with ORDER BY in MySQL?
- How to sort by value with MySQL ORDER BY?
- Fetch substrings from a string with words separated by slash in MySQL?
- Parse a string to get a number from a large string separated by underscore
Advertisements