- 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
How do I sort numbers saved as VARCHAR in MySQL with some of them having preceding 0 like 085, 090, etc.?
Following is the syntax −
select *from yourTableName order by yourColumnName*1,yourColumnName;
Let us first create a table −
mysql> create table DemoTable ( Value varchar(100) ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('90'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('86'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('45'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('85'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('085'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('090'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('045'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 90 | | 86 | | 45 | | 85 | | 085 | | 090 | | 045 | +-------+ 7 rows in set (0.00 sec)
Following is the query to sort numbers saved as VARCHAR with some of them having to precede 0
mysql> select *from DemoTable order by Value*1,Value;
This will produce the following output −
+-------+ | Value | +-------+ | 045 | | 45 | | 085 | | 85 | | 86 | | 090 | | 90 | +-------+ 7 rows in set (0.00 sec)
- Related Articles
- How to display highest value from a string with numbers set as varchar in MySQL?
- MySQL query to replace backslash from a varchar column with preceding backslash string values
- Fetch maximum value from a column with values as string numbers like Value440, Value345, etc. in SQL
- Count numbers having 0 as a digit in C++
- MySQL query to fetch records with arrangement in the form of numbers and letter like 99S, 50K, etc.?
- Set numbers as a varchar field and compare in MySQL
- Using UNIQUE for varchar columns with some conditions in MySQL?
- Delete last value and fix two new values (VARCHAR Numbers) in MySQL declared as VARCHAR?
- Order VARCHAR records with string and numbers in MySQL
- How to set a string with hyphen and numbers in MySQL varchar?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- I used ‘from’ and ‘to’ with backticks as column titles in my database table. Now, how do I SELECT them?
- How to sort varchar numeric columns by DESC or ASC in MySQL?
- How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
- How do I sort natural in MongoDB?

Advertisements