

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display highest value from a string with numbers set as varchar in MySQL?
For this, you need to cast the varchar value to INTEGER.
Let us first create a table −
mysql> create table DemoTable765 (ItemPrice varchar(200)); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable765 values('567.00'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable765 values('1089.00'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable765 values('540.00'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable765 values('788.00'); Query OK, 1 row affected (0.39 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable765;
This will produce the following output -
+-----------+ | ItemPrice | +-----------+ | 567.00 | | 1089.00 | | 540.00 | | 788.00 | +-----------+ 4 rows in set (0.00 sec)
Following is the query to display highest value from a string with numbers. We have used CAST() to cast it to INTEGER −
mysql> select *from DemoTable765 order by cast(ItemPrice AS SIGNED INTEGER) DESC;
This will produce the following output -
+-----------+ | ItemPrice | +-----------+ | 1089.00 | | 788.00 | | 567.00 | | 540.00 | +-----------+ 4 rows in set, 4 warnings (0.00 sec)
- Related Questions & Answers
- How to set a string with hyphen and numbers in MySQL varchar?
- Set numbers as a varchar field and compare in MySQL
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Delete last value and fix two new values (VARCHAR Numbers) in MySQL declared as VARCHAR?
- Order VARCHAR records with string and numbers in MySQL
- Find maximum value from a VARCHAR column in MySQL
- MySQL order by from highest to lowest value?
- How to get a value more than a particular value from varchar column in MySQL?
- Only display row with highest ID in MySQL
- Display highest amount from corresponding duplicate ids in MySQL
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- Getting the maximum value from a varchar field in MySQL
- MySQL query to get the highest value from a single row with multiple columns
- How to display a single quote text as a column value in MySQL?
- How to order by the highest value from two columns in MySQL?
Advertisements