- 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
Ordering string as a number in a database?
To order string as a number, use CAST(). Following is the syntax −
select *from yourTableName ORDER BY CAST(yourColumnName AS SIGNED) DESC;
Let us first create a table −
mysql> create table DemoTable ( Id varchar(100) ); Query OK, 0 rows affected (0.18 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('3'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('20'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('34'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('21'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values('78'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable values('90'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+ | Id | +------+ | 3 | | 20 | | 34 | | 21 | | 78 | | 90 | +------+ 6 rows in set (0.00 sec)
Following is the query to ordering integer numbers −
mysql> select *from DemoTable ORDER BY CAST(Id AS SIGNED) DESC;
This will produce the following output −
+------+ | Id | +------+ | 90 | | 78 | | 34 | | 21 | | 20 | | 3 | +------+ 6 rows in set (0.02 sec)
- Related Articles
- Ordering string in an array according to a number in the string JavaScript
- Quickly search for a string in MySQL database?
- Remove leading zeros from a Number given as a string using Python
- Should I store a field PRICE as an int or as a float in the database?
- Which MySQL function returns a specified number of characters of a string as output?
- How to find and replace string in MySQL database for a particular string only?
- Represent Int64 as a String in C#
- Represent Int32 as a String in C#
- Print a number as string of 'A' and 'B' in lexicographic order in C++
- Evaluating a string as a mathematical expression in JavaScript
- Weekday as a number in JavaScript?
- Divide large number represented as string in C++ Program
- Set Column Ordering in Bootstrap
- JavaScript program to take in a binary number as a string and returns its numerical equivalent in base 10
- Validate a number as Fibonacci series number in JavaScript

Advertisements