

- 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
How to sort an alphanumeric column with different lengths in MySQL?
Let us first create a table −
mysql> create table DemoTable1623 -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1623 values('STU-MIT-143'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1623 values('STU-MIT-10'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1623 values('STU-MIT-150'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1623 values('STU-MIT-148'); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1623;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | STU-MIT-143 | | STU-MIT-10 | | STU-MIT-150 | | STU-MIT-148 | +-------------+ 4 rows in set (0.00 sec)
Here is the query to sort an alphanumeric column with different lengths in MySQL.
mysql> select * from DemoTable1623 -> order by substring_index(StudentCode, '-', 1), -> substring_index(StudentCode, '-', -1) + 0;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | STU-MIT-10 | | STU-MIT-143 | | STU-MIT-148 | | STU-MIT-150 | +-------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- How to sort an alphanumeric column in MySQL?
- How to order an alphanumeric column in MySQL?
- How to do alphanumeric sort in MongoDB?
- Sort only numbers from alphanumeric string in MySQL?
- Sort strings in Alphanumeric sequence
- Sort an array of strings according to string lengths in C++
- How to sort mixed numeric/alphanumeric array in JavaScript
- Plotting two different arrays of different lengths in matplotlib
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- How to create boxplot of vectors having different lengths in R?
- How to select different values from same column and display them in different columns with MySQL?
- How to generate passwords with varying lengths in R?
- How to create a duplicate column in an R data frame with different name?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- How to randomly split a vector into n vectors of different lengths in R?
Advertisements