![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 records with special characters like '2321/78/54-6'
To sort, use ORDER BY SUBSTRING(). Let us first create a table −
mysql> create table DemoTable -> ( -> Value varchar(40) -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2321/78/54-6') -> ; Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2321/78/54-8'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2321/78/54-5'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2321/78/54-9'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------------+ | Value | +--------------+ | 2321/78/54-6 | | 2321/78/54-8 | | 2321/78/54-5 | | 2321/78/54-9 | +--------------+ 4 rows in set (0.00 sec)
Here is the query to sort records with special characters −
mysql> select * from DemoTable -> where Value like '2321/78/54%' -> order by substring(Value from 1 for 10),cast(substring(Value from 11) as decimal);
This will produce the following output −
+--------------+ | Value | +--------------+ | 2321/78/54-9 | | 2321/78/54-8 | | 2321/78/54-6 | | 2321/78/54-5 | +--------------+ 4 rows in set (0.05 sec)
Advertisements