
- 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
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 Articles
- 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?
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- How to sort mixed numeric/alphanumeric array in JavaScript
- Sort strings in Alphanumeric sequence
- How to select different values from same column and display them in different columns with MySQL?
- Sort an array of strings according to string lengths in C++
- How to create a duplicate column in an R data frame with different name?
- How to sort more than one column at a time in MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- How to create boxplot of vectors having different lengths in R?
- Get all the records with two different values in another column with MySQL
- Sort data column to retrieve max textual value in MySQL

Advertisements