How to use the MySQL MAKETIME() function

MySQL MAKETIME() is a function that creates and returns a time value based on an hour, minute, and second value.

Posted on

MySQL MAKETIME() is a function that creates and returns a time value based on an hour, minute, and second value. It can be used to generate custom time values with specific hour, minute, and second components. The function takes three arguments: the hour value, the minute value, and the second value. The return value is a time value in the format ‘HH:MM:SS’ or HHMMSS, depending on whether the function is used in a string or numeric context. The function returns NULL if any argument is invalid or NULL.

Syntax

The syntax of the function is:

MAKETIME(hour, minute, second)

The parameters are:

  • hour: The hour value of the time, which is an integer value between 0 and 23.
  • minute: The minute value of the time, which is an integer value between 0 and 59.
  • second: The second value of the time, which can have a fractional part, so it allows specifying fractions of a second.

Examples

Some examples of using the function are:

  • To create and return a time value based on an hour, minute, and second value, use:

    SELECT MAKETIME(11, 35, 4);
    

    The result is:

    11:35:04
    

    This means that the time value is 11 hours, 35 minutes, and 4 seconds.

  • To create and return a time value based on an hour, minute, and second value in numeric format, use:

    SELECT MAKETIME(16, 1, 0) + 0;
    

    The result is:

    160100
    

    This means that the time value is 160100.

  • To create and return a time value based on an hour, minute, and second value in a different format, use:

    SELECT DATE_FORMAT(MAKETIME(21, 59, 59), '%h:%i:%s %p');
    

    The result is:

    09:59:59 PM
    

    This means that the time value is formatted as hour:minute:second AM/PM, using the 12-hour clock.

  • To create and return a time value based on an hour, minute, and second value that exceeds the normal range, use:

    SELECT MAKETIME(838, 59, 59);
    

    The result is:

    838:59:59
    

    This means that the time value is 838 hours, 59 minutes, and 59 seconds. Note that the hour value can be greater than 23, but the minute and second values cannot be greater than 59.

  • To create and return a time value based on an hour, minute, and second value that has a fractional part, use:

    SELECT MAKETIME(12, 15, 30.5);
    

    The result is:

    12:15:30.5
    

    This means that the time value is 12 hours, 15 minutes, and 30.5 seconds.

Similar Functions

Some similar functions to MAKETIME() are:

  • TIME(): This function returns the time part of a datetime expression.
  • TIME_FORMAT(): This function formats a time value according to a specified format string.
  • SEC_TO_TIME(): This function converts a number of seconds to a time value.
  • TIME_TO_SEC(): This function converts a time value to a number of seconds.