
- 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
Is it possible to sort varchar data in ascending order that have both string and number values with MySQL?
For this, you can use ORDER BY IF(CAST()). Let us first create a table −
mysql> create table DemoTable(EmployeeCode varchar(100)); Query OK, 0 rows affected (1.17 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('190'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('100'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('120'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | EmployeeCode | +--------------+ | 190 | | 100 | | John | | 120 | +--------------+ 4 rows in set (0.00 sec)
Following is the query to sort varchar data in ascending order with both string and number values −
mysql> select *from DemoTable ORDER BY IF(CAST(EmployeeCode AS SIGNED) = 0, 100000000000, CAST(EmployeeCode AS SIGNED));
This will produce the following output. Here, numbers get sorted first −
+--------------+ | EmployeeCode | +--------------+ | 100 | | 120 | | 190 | | John | +--------------+ 4 rows in set, 1 warning (0.00 sec)
- Related Articles
- Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
- Order VARCHAR records with string and numbers in MySQL
- Sort values that contain letters and symbols in custom order with MySQL
- MySQL command to order timestamp values in ascending order?
- How can we sort MySQL output in ascending order?
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- Is it possible to have View and table with the same name in MySQL?
- Sort the MongoDB documents in ascending order with aggregation?
- MySQL Order by a specific column x and display remaining values in ascending order
- In MySQL, how it can be possible to specify a sort order using a column that is not retrieved by the query?
- How to perform ascending order sort in MongoDB?
- Is it okay to store double and date in VARCHAR with MySQL?
- Sort index in ascending order – Python Pandas
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
- How to sort Java array elements in ascending order?

Advertisements