
- 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 in MySQL?
To sort an alphanumeric column, use LIKE operator along with SUBSTRING(). Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (1.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('S/TU/100'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('S/TU/1000'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('S/TU/10'); Query OK, 1 row affected (0.47 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+ | StudentId | +-----------+ | S/TU/100 | | S/TU/1000 | | S/TU/10 | +-----------+ 3 rows in set (0.00 sec)
Here is the query to sort an alphanumeric column in MySQL −
mysql> select StudentId from DemoTable where StudentId LIKE 'S%' order by substring(StudentId,1,9), substring(StudentId,10)*1;
Output
This will produce the following output −
+-----------+ | StudentId | +-----------+ | S/TU/10 | | S/TU/100 | | S/TU/1000 | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- How to sort an alphanumeric column with different lengths 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 sort mixed numeric/alphanumeric array in JavaScript
- Sort strings in Alphanumeric sequence
- How to sort more than one column at a time in MySQL?
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- Sort data column to retrieve max textual value in MySQL
- How to sort a numerical factor column in an R data frame?
- How to remove all non-alphanumeric characters from a string in MySQL?
- Sort a column ignoring a specific word in MySQL
- Delete duplicate alphanumeric entries from column data in SQL
- How to sort an R data frame column without losing row names?
- How to rename a column in an existing MySQL table?

Advertisements