MySQL MINUTE

The MINUTE function in MySQL extracts the minute component from a given time or datetime value. It returns an integer value ranging from 0 to 59, corresponding to the minutes in the specified time. This function is commonly used in conjunction with other date and time functions to perform various data analysis and manipulation tasks.

Syntax

Here is the MINUTE function syntax:

MINUTE(expr)

expr: This is the expression from which you want to extract the minute.

Examples

Extract minute from a datetime

SELECT MINUTE('2022-01-01 12:34:56');

This query would return 34 because it extracts the minute part from the given datetime.

Here’s a simple breakdown:

2022-01-01 12:34:56: The input datetime.
34: The minute part extracted.

Filter a table based on the minute

SELECT * 
FROM orders
WHERE MINUTE(order_time) = 15;

This will select all orders that were placed at 15 minute.

Calculate the difference in minutes between two timestamps

SELECT 
MINUTE(end_time) - MINUTE(start_time) AS time_diff
FROM activities;

This will calculate the time difference in minutes for all activities in the activities table.

Keep in mind that the MySQL MINUTE function can be useful in various scenarios, such as when you need to perform calculations based on the minute component of a timestamp or display information at a more granular level.