- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Getting the maximum value from a varchar field in MySQL
Use MAX() function along with SUBSTRING() for this. Let us first create a table −
mysql> create table DemoTable -> ( -> Id varchar(200) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-0515-1980'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable values('2019-0516-780'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-0517-2780'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----------------+ | Id | +----------------+ | 2019-0515-1980 | | 2019-0516-780 | | 2019-0517-2780 | +----------------+ 3 rows in set (0.00 sec)
Following is the query to get the maximum value from a varchar field −
mysql> select max(Id) from DemoTable -> where Id LIKE CONCAT(SUBSTRING(Id, 1, 4),'%');
Output
+----------------+ | max(Id) | +----------------+ | 2019-0517-2780 | +----------------+ 1 row in set (0.00 sec)
- Related Articles
- Find maximum value from a VARCHAR column in MySQL
- Searching for an integer value in a varchar field in MySQL?
- Getting Minimum and Maximum Value in MySQL
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Get maximum date from a list of varchar dates in MySQL
- Sorting varchar field numerically in MySQL?
- MySQL query to get the max value with numeric values in varchar field?
- Can we compare numbers in a MySQL varchar field?
- How to get a value more than a particular value from varchar column in MySQL?
- Getting maximum from a column value and set it for all other values in the same column with MySQL?
- Set numbers as a varchar field and compare in MySQL
- Fetch the maximum value from a MySQL column?
- How to derive value of a field from another field in MySQL?
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- What is the maximum length of MySQL VARCHAR column?

Advertisements