Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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)
Advertisements
