
- 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
Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
Use MIN() function along with SUBSTRING() for minimum, whereas MAX() for maximum. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value varchar(100) -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values('10-20'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Value) values('200-100'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values('780-235'); Query OK, 1 row affected (0.29 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+---------+ | Id | Value | +----+---------+ | 1 | 10-20 | | 2 | 200-100 | | 3 | 780-235 | +----+---------+ 3 rows in set (0.00 sec)
Following is the query to find the MIN() and MAX() value from the number substrings separated from a string with hyphen −
mysql> select min(SUBSTRING_INDEX(Value, '-', 1)), -> max(SUBSTRING_INDEX(Value, '-', -1)) -> from DemoTable;
This will produce the following output −
+-------------------------------------+--------------------------------------+ | min(SUBSTRING_INDEX(Value, '-', 1)) | max(SUBSTRING_INDEX(Value, '-', -1)) | +-------------------------------------+--------------------------------------+ | 10 | 235 | +-------------------------------------+--------------------------------------+ 1 row in set (0.04 sec)
- Related Articles
- REGEX in MySQL to display only numbers separated by hyphen.
- MySQL left padding with zeros separated by hyphen?
- How to set a string with hyphen and numbers in MySQL varchar?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Fetch substrings from a string with words separated by slash in MySQL?
- How to concatenate string vectors separated with hyphen in R?
- How to sum a comma separated string (string with numbers) in MySQL?
- Getting Minimum and Maximum Value in MySQL
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Order by numeric value from string records separated by numbers like CSE 15, CSE 11, etc.?
- Two ways to fetch maximum value from a MySQL column with numbers
- Select the minimum value from the maximum values of two tables with a single MySQL\nquery?
- Maximum and minimum sums from two numbers with digit replacements in C++
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- MySQL query to get a single value from position of comma-separated string?

Advertisements