How to get a value more than a particular value from varchar column in MySQL?


Since the column wherein you want to get the value more than a particular value is VARCHAR, use the CAST() function. For example, to fetch the value more than 999 from a column with varchar values.

Let us first create a table −

mysql> create table DemoTable
(
   Value varchar(100)
);
Query OK, 0 rows affected (1.02 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('900');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('1090');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('860');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('12345');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('908345');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Value  |
+--------+
| 900    |
| 1090   |
| 860    |
| 12345  |
| 908345 |
+--------+
5 rows in set (0.00 sec)

Following is the query to get a value more than a particular value from varchar column −

mysql> select max(cast(Value AS SIGNED)) from DemoTable;

This will produce the following output −

+----------------------------+
| max(cast(Value AS SIGNED)) |
+----------------------------+
|                     908345 |
+----------------------------+
1 row in set (0.05 sec)

Updated on: 24-Sep-2019

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements