
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to sort varchar numeric columns by DESC or ASC in MySQL?
Let us first create a table −
mysql> create table DemoTable726 (Value varchar(100)); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable726 values('100'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable726 values('10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable726 values('110'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable726 values('2000'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable726 values('1000'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable726;
This will produce the following output -
+-------+ | Value | +-------+ | 100 | | 10 | | 110 | | 2000 | | 1000 | +-------+ 5 rows in set (0.00 sec)
Following is the query to sort varchar numeric columns in ascending order −
mysql> select *from DemoTable726 order by CAST(Value AS DECIMAL(20,2));
This will produce the following output -
+-------+ | Value | +-------+ | 10 | | 100 | | 110 | | 1000 | | 2000 | +-------+ 5 rows in set (0.00 sec)
Following is the query to sort and display the result in descending order −
mysql> select *from DemoTable726 order by CAST(Value AS DECIMAL(20,2)) DESC;
This will produce the following output -
+-------+ | Value | +-------+ | 2000 | | 1000 | | 110 | | 100 | | 10 | +-------+ 5 rows in set (0.00 sec)
- Related Articles
- Implement MySQL ORDER BY without using ASC or DESC?
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- How to select only non - numeric values from varchar column in MySQL?
- Order By date ASC in MySQL?
- What would be the effect on summary output when I use explicit sort order (ASC or DESC) with column names in the GROUP BY list along with “WITH ROLLUP” modifier?
- MySQL query to get the max value with numeric values in varchar field?
- How to Sort CSV by multiple columns in Python ?
- How to Auto-Sort Columns by Value in Excel?
- How to perform numeric sort in JavaScript?
- How to find numeric columns in Pandas?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- MySQL query to get max id from varchar type and the values in numeric?
- Order by desc except a single value in MySQL
- Using UNIQUE for varchar columns with some conditions in MySQL?
- How to sum rows of VARCHAR datatype or TIME datatype in MySQL?

Advertisements