
- DBMS Tutorial
- DBMS - Home
- DBMS - Overview
- DBMS - Architecture
- DBMS - Data Models
- DBMS - Data Schemas
- DBMS - Data Independence
- Entity Relationship Model
- DBMS - ER Model Basic Concepts
- DBMS - ER Diagram Representation
- DBMS - Generalization, Aggregation
- Relational Model
- DBMS - Codd's Rules
- DBMS - Relational Data Model
- DBMS - Relational Algebra
- DBMS - ER to Relational Model
- DBMS- SQL Overview
- Relational Database Design
- DBMS - Database Normalization
- DBMS - Database Joins
- Storage and File Structure
- DBMS - Storage System
- DBMS - File Structure
- Indexing and Hashing
- DBMS - Indexing
- DBMS - Hashing
- Transaction And Concurrency
- DBMS - Transaction
- DBMS - Concurrency Control
- DBMS - Deadlock
- Backup and Recovery
- DBMS - Data Backup
- DBMS - Data Recovery
- DBMS Useful Resources
- DBMS - Quick Guide
- DBMS - Useful Resources
- DBMS - Discussion
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)
- Related Articles
- MySQL Order by beginning letter?
- Advanced sorting in MySQL to display strings beginning with J at the end even after ORDER BY
- MySQL Order by with case?
- MySQL query to display a substring before a special character in a string
- MySQL ORDER BY with CASE WHEN
- MySQL order by field using CASE Statement
- How to ORDER BY last 2 character string in MySQL?
- MySQL order by 0 first and then display the record in descending order?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- MySQL ORDER BY ASC and display NULLs at the bottom?
- MySQL Order by a specific column x and display remaining values in ascending order
- Get the names beginning with a particular character using LIKE in MySQL
- Order by a single field and display rest of the records in the same order with MySQL
- How to make Android character by character display text animation?
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character

Advertisements