Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('190');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('230');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('120');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('189');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Value |
+-------+
|   190 |
|   230 |
|   120 |
|   189 |
+-------+
4 rows in set (0.00 sec)

Following is the query to get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns −

mysql> select min(cast(Value AS SIGNED)) AS Min,
max(cast(Value AS SIGNED)) AS Max
from DemoTable;

This will produce the following output −

+------+------+
| Min  | Max  |
+------+------+
|  120 | 230  |
+------+------+
1 row in set (0.03 sec)
raja
Published on 26-Sep-2019 11:19:48
Advertisements