
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- 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
- How to get first N characters from a MySQL column?
- How to sort an alphanumeric column with different lengths in MySQL?
- MySQL CONCAT a specific column value with the corresponding record
- Get beginning and end date from a specific year in MySQL
- MongoDB query to get record beginning with specific element from an array?
- Get only the file extension from a column with file names as strings in MySQL?
- MySQL LIKE command doesn't work with strings containing dots to display records beginning with a specific number
- How to remove all non-alphanumeric characters from a string in MySQL?
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- Alphanumeric Order by in MySQL for strings mixed with numbers
- Fetch the maximum value from a MySQL column?

Advertisements