SQL Server `MINUTE()` Function
In SQL Server, the MINUTE()
function is used to extract the minute part of a time value. It can be used in conjunction with other functions such as DATEADD() and DATEDIFF() for more complex time calculations.
Syntax
MINUTE(date)
Here, date
is the date/time value from which to extract the minute part.
Use Cases
The MINUTE()
function is commonly used in queries that involve working with time values, such as:
- Calculating the number of minutes between two times
- Extracting the minute part of a time value
Examples
Here are two examples of using the MINUTE()
function:
Example 1
Suppose we have a table called Orders
with a datetime column called OrderDate
. Now, we want to calculate the number of minutes between January 1, 2022, and the order date.
SELECT DATEDIFF(minute, '2022-01-01', OrderDate) AS MinutesDiff
FROM Orders;
The above query will return a result set that contains the minute difference between each order and January 1, 2022.
Example 2
Now, suppose we want to extract the minute part from a date/time value.
SELECT MINUTE('2022-03-11 15:30:45') AS MinuteValue;
The above query will return the minute part of the time value 15:30:45
, which is 30
.
Conclusion
The MINUTE()
function is a useful function in SQL Server for extracting the minute part of a time value. It can be used in conjunction with other functions to perform more complex time calculations. In queries that involve working with time values, the MINUTE()
function can provide convenience and flexibility.