- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 letters (not numbers) for column values comprising strings with numbers like '456 John Smith'
To ORDER BY letters, use ORDER BY SUBSTRING(). Let us first create a table −
mysql> create table DemoTable ( Id varchar(100) ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('456 John Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('897 Adam Smith'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('1009 Bob Smith'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------------+ | Id | +----------------+ | 456 John Smith | | 897 Adam Smith | | 1009 Bob Smith | +----------------+ 3 rows in set (0.00 sec)
Following is the query to ORDER BY letters −
mysql> select *from DemoTable order by SUBSTRING(Id,LOCATE(' ', Id));
This will produce the following output −
+----------------+ | Id | +----------------+ | 897 Adam Smith | | 1009 Bob Smith | | 456 John Smith | +----------------+ 3 rows in set (0.05 sec)
- Related Articles
- Alphanumeric Order by in MySQL for strings mixed with numbers
- MySQL order by string with numbers?
- Smith Numbers in java
- MySQL ORDER BY strings with underscore?
- Multiple LIKE Operators with ORDER BY in MySQL?
- Sort values that contain letters and symbols in custom order with MySQL
- MySQL select order by acts like a string (not a number)?
- MySQL Order By specific strings?
- How to set country code to column values with phone numbers in MySQL?
- Sort character values from a column mixed with character and numbers in MySQL?
- Fetch maximum value from a column with values as string numbers like Value440, Value345, etc. in SQL
- Replacing strings with numbers in Python for Data Analysis
- Order VARCHAR records with string and numbers in MySQL
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- MySQL Order by a specific column x and display remaining values in ascending order

Advertisements