
- 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
Sort character values from a column mixed with character and numbers in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Code varchar(20) -> ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('J23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('C13'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('A61'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('D56'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+------+ | Code | +------+ | J23 | | C13 | | A61 | | D56 | +------+ 4 rows in set (0.00 sec)
Here is the query to sort character values −
mysql> select * from DemoTable -> order by left(Code,1), -> cast(substr(Code, 2) as unsigned);
This will produce the following output −
+------+ | Code | +------+ | A61 | | C13 | | D56 | | J23 | +------+ 4 rows in set (0.05 sec)
- Related Articles
- Add a character in the end to column values with MySQL SELECT?
- Sort in a string mixed with numbers in MySQL?
- MySQL query to select column values ending with certain character/number?
- Sort by character length in MySQL
- MySQL query to get the character length for all the values in a column?
- Replace numerical column values based on character column values in R data frame.
- How to replace a particular character in a MySQL column?
- How to remove all instances of a specific character from a column in MySQL?
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- Find sum by removing first character from a string followed by numbers in MySQL?
- Filter column value by the first character in MySQL
- MySQL query to sort by certain last string character?
- How to create a column with the serial number of values in character column of an R data frame?
- Get field value and convert a particular character from it to uppercase with MySQL
- How to associate a character to numbers in an R data frame column?

Advertisements