Summary: in this tutorial, you will learn how to use the MySQL MINUTE()
function to get the minute for a time value.
Introduction to MySQL MINUTE() function
The MINUTE()
function allows you to extract the minute component of a time value.
Here’s the basic syntax of the MINUTE()
function:
MINUTE(time)
Code language: SQL (Structured Query Language) (sql)
In this syntax:
time
: The time that you want to extract the minute component. The time value has the data type isTIME
.
The MINUTE()
function returns an integer that represents the minute component of the time.
If the time represents the time of the day, the MINUTE()
function returns a value between 0 and 59. But if the time value is larger, the MINUTE()
function can return values greater than 59.
If the time is NULL
, the MINUTE()
function returns NULL
.
MySQL MINUTE() function examples
Let’s take some examples of using the MINUTE()
function.
1) Simple MINUTE() function example
The following example uses the MINUTE()
function to get the minute from the time '10:45:20'
:
SELECT MINUTE('10:45:20');
Code language: SQL (Structured Query Language) (sql)
Output:
+--------------------+
| MINUTE('10:45:20') |
+--------------------+
| 45 |
+--------------------+
1 row in set (0.00 sec)
Code language: plaintext (plaintext)
2) Using MINUTE() function with table data
First, create a new table called appointments
with the following structure:
CREATE TABLE appointments (
id INT PRIMARY KEY AUTO_INCREMENT,
time DATETIME
);
Code language: SQL (Structured Query Language) (sql)
The appointments
table is simplified for brevity purposes.
Second, insert some rows into the orders
table:
INSERT INTO appointments (time)
VALUES
('2023-10-18 08:15:00'),
('2023-10-18 08:30:00'),
('2023-10-18 08:45:00'),
('2023-10-18 09:00:00'),
('2023-10-18 09:15:00');
Code language: SQL (Structured Query Language) (sql)
Third, get the number of appointments by minutes using the MINUTE()
function:
SELECT
MINUTE(time) AS scheduled_minute,
COUNT(*) AS appointment_count
FROM
appointments
GROUP BY
scheduled_minute;
Code language: SQL (Structured Query Language) (sql)
Output:
+------------------+-------------------+
| scheduled_minute | appointment_count |
+------------------+-------------------+
| 15 | 2 |
| 30 | 1 |
| 45 | 1 |
| 0 | 1 |
+------------------+-------------------+
4 rows in set (0.00 sec)
Code language: plaintext (plaintext)
Summary
- Use the
MINUTE()
function to get the minute component of a specific time.