MAKETIME() Examples – MySQL

When using MySQL, you can use the MAKETIME() function to return a time from the various time parts.

In other words, you provide three arguments; the hour, the minutes, and the seconds. The MAKETIME() function will then return the time value based on those two arguments.

Syntax

The syntax goes like this:

MAKETIME(hour,minute,second)

Where hour is the hour part, minute is the minutes part, and second is the seconds part.

Example 1 – Basic Usage

Here’s an example to demonstrate.

SELECT MAKETIME(10,35,17);

Result:

+--------------------+
| MAKETIME(10,35,17) |
+--------------------+
| 10:35:17           |
+--------------------+

Example 2 – Fractional Seconds

The seconds argument can also have a fractional part.

SELECT MAKETIME(10,35,17.123456);

Result:

+---------------------------+
| MAKETIME(10,35,17.123456) |
+---------------------------+
| 10:35:17.123456           |
+---------------------------+

Example 3 – A Larger Hour Part

The hour part is not restricted to the 0 to 23 range. The time could possibly represent elapsed time or time passed between two events.

SELECT MAKETIME(100,35,17);

Result:

+---------------------+
| MAKETIME(100,35,17) |
+---------------------+
| 100:35:17           |
+---------------------+

However, this doesn’t apply to the minutes part. It needs to be within the 0 to 59 range:

SELECT 
    MAKETIME(10,-1,17),
    MAKETIME(10,60,17);

Result:

+--------------------+--------------------+
| MAKETIME(10,-1,17) | MAKETIME(10,60,17) |
+--------------------+--------------------+
| NULL               | NULL               |
+--------------------+--------------------+

The same applies to the seconds part:

SELECT 
    MAKETIME(10,35,-1),
    MAKETIME(10,35,60);

Result:

+--------------------+--------------------+
| MAKETIME(10,35,-1) | MAKETIME(10,35,60) |
+--------------------+--------------------+
| NULL               | NULL               |
+--------------------+--------------------+