- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Find maximum value from a VARCHAR column in MySQL
To find maximum value, use MAX() along with CAST(), since the values are of VARCHAR type. Let us first create a table −
mysql> create table DemoTable2030 -> ( -> Value varchar(20) -> ); Query OK, 0 rows affected (0.44 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2030 values('8'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2030 values('1'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2030 values('10001'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2030 values('901'); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement &miuns;
mysql> select *from DemoTable2030
This will produce the following output −
+-------+ | Value | +-------+ | 8 | | 1 | | 10001 | | 901 | +-------+ 4 rows in set (0.00 sec)
Following is the query to find maximum value from a VARCHAR column −
mysql> select max(cast(Value as unsigned)) from DemoTable2030;
This will produce the following output −
+------------------------------+ | max(cast(Value as unsigned)) | +------------------------------+ | 10001 | +------------------------------+ 1 row in set (0.00 sec)
Advertisements