MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
N.M.P.L. Company www.NearMePayday.Loan || 3-month (90 days)
Powered by MySQL
 
Home / Tricky Select Queries / Question No: 2

How to do numeric sorting within a string field in MySQL?

Is there a way to do a numeric sorting when the number exists in a string. To be clear more, take an example:
Radius 1200
Radius 1500
Radius 1800
Radius 300
Radius 600
Radius 900

Being that character wise 1 is before 3, it is wondering if there's a way through the database to achieve the above record in ascending order as below:
Radius 300
Radius 600
Radius 900
Radius 1200
Radius 1500

Answer No: 2

This can be possible. Example your table is test, and the column is test_col. Use the following, and if the number is always at the end and always has a space in front of it this will work:
SELECT test_col, SUBSTRING_INDEX(test_col, '', 1) as test_col_str, 
SUBSTRING_INDEX(test_col, ' ', -1) + 0 as test_col_num
FROM test ORDERBY test_col_str, test_col_num;

Related MySQL FAQs to the Above FAQ

How-to-take-data-back-up-of-single-table How to take data back up of single table?

How-to-find-duplicate-records-from-a-table-in-MySQL How to find duplicate records from a table in MySQL?

How-to-get-last-top-records-in-asceding-order How to get last top records in asceding order?

How-to-move-table-from-one-to-another-MySQL-database How to move table from one to another MySQL database?

How-to-get-parent-table-child-table-and-grandchild-table-aggregates-in-MySQL How to get parent table child table and grandchild table aggregates in MySQL?

How-to-skip-repeating-values-from-the-query-results How to skip repeating values from the query results?

How-to-retrieve--one-child-row-against-each-parent-id How to retrieve one child row against each parent id?

An-important-trick-to-speed-up-select-query An important trick to speed up select query.

How-to-find-the-books-which-have-authors-with-more-than-one-book-in-the-table How to find the books which have authors with more than one book in the table?

How-to-count-the-number-of-rows-containing-APOSTROPHE-in-MySQL How to count the number of rows containing APOSTROPHE in MySQL?

How-to-compare-data-of-two-tables-of-two-different-databases-in-MySQL How to compare data of two tables of two different databases in MySQL?

How-to-retreive-multiple-records-without-a-reference-table How to retreive multiple records without a reference table?

About FAQs: Recently Added FAQs

About MySQL FAQs: Site Map | Bookmark Us | Recommend this Site to Your Friend | Contact Us

Useful Links: Wikipedia.org | Oracle.com | w3schools.com | www.php.net | Github.com

© 2021  www.mysqlfaqs.net
All rights reserved.