

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
For maximum value, use MAX() along with CAST() for conversion. Since we want maximum value from string-numbers beginning with specific characters, use RLIKE. Let us first create a table −
mysql> create table DemoTable1381 -> ( -> DepartmentId varchar(40) -> ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1381 values('IT794'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1381 values('AT1034'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1381 values('IT967'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1381 values('IT874'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1381 values('AT967'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1381;
This will produce the following output −
+--------------+ | DepartmentId | +--------------+ | IT794 | | AT1034 | | IT967 | | IT874 | | AT967 | +--------------+ 5 rows in set (0.00 sec)
Following is the query to get the maximum value from a column with alphanumeric strings beginning with specific characters i.e. “IT” here −
mysql> select max(cast(substr(trim(DepartmentId),3) AS UNSIGNED)) from DemoTable1381 where DepartmentId RLIKE 'IT';
This will produce the following output −
+-----------------------------------------------------+ | max(cast(substr(trim(DepartmentId),3) AS UNSIGNED)) | +-----------------------------------------------------+ | 967 | +-----------------------------------------------------+ 1 row in set (0.10 sec)
- Related Questions & Answers
- How to get the maximum value from strings with integers in MySQL?
- How to get the count of a specific value in a column with MySQL?
- Get the maximum value of a column with MySQL Aggregate function
- Two ways to fetch maximum value from a MySQL column with numbers
- MySQL CONCAT a specific column value with the corresponding record
- Fetch the maximum value from a MySQL column?
- Get beginning and end date from a specific year in MySQL
- MongoDB query to get record beginning with specific element from an array?
- How to get first N characters from a MySQL column?
- Get only the file extension from a column with file names as strings in MySQL?
- How to sort an alphanumeric column with different lengths in MySQL?
- How to remove all non-alphanumeric characters from a string in MySQL?
- Alphanumeric Order by in MySQL for strings mixed with numbers
- MySQL LIKE command doesn't work with strings containing dots to display records beginning with a specific number
- Finding only strings beginning with a number using MySQL Regex?
Advertisements