![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
MySQL ORDER BY CASE to display special character in the beginning
Let us first create a table −
mysql> create table DemoTable ( StudentId varchar(40) ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('~'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('40'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | 10 | | 20 | | ~ | | NULL | | 40 | | NULL | +-----------+ 6 rows in set (0.00 sec)
Following is the query to special character sort in MySQL −
mysql> select *from DemoTable order by case when StudentId not like '%[^a-zA-Z0-9]%' THEN 80 when StudentId is null then 98 else 85 end,StudentId;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | ~ | | 10 | | 20 | | 40 | | NULL | | NULL | +-----------+ 6 rows in set (0.03 sec)
Advertisements