How the MAKETIME() function works in Mariadb?

The MAKETIME() function is a time function that returns a time value that is composed of a given hour, minute, and second.

Posted on

The MAKETIME() function is a time function that returns a time value that is composed of a given hour, minute, and second. The MAKETIME() function is useful for creating a time value from a given hour, minute, and second.

Syntax

The syntax of the MAKETIME() function is as follows:

MAKETIME(hour, minute, second)

The hour argument is an integer value that specifies the hour of the time. The minute argument is an integer value that specifies the minute of the time. The second argument is a decimal value that specifies the second of the time. The second argument can have fractional parts, such as 1.5 or 2.75.

The MAKETIME() function returns a time value that is composed of the hour, minute, and second arguments. If any of the arguments is NULL, the function returns NULL.

The time range supported by MariaDB is from -838:59:59 to 838:59:59.

Examples

Example 1: Basic usage of the MAKETIME() function

The following example shows how to use the MAKETIME() function with a simple hour, minute, and second. It returns a time value that is composed of the hour 12, the minute 34, and the second 56.

SELECT MAKETIME(12, 34, 56);

The output is:

12:34:56

This means that the MAKETIME() function returns the time value that corresponds to the hour 12, the minute 34, and the second 56.

Example 2: Using the MAKETIME() function with a fractional second

The following example shows how to use the MAKETIME() function with a fractional second. It returns a time value that is composed of the hour 12, the minute 34, and the second 56.78.

SELECT MAKETIME(12, 34, 56.78);

The output is:

12:34:56.78

This means that the MAKETIME() function returns the time value that corresponds to the hour 12, the minute 34, and the second 56.78.

Example 3: Using the MAKETIME() function with an out of range hour

The following example shows what happens when the MAKETIME() function is used with an out of range hour.

SELECT MAKETIME(25, 34, 56);

The output is:

NULL

This means that the MAKETIME() function returns 25:34:56 because the time range supported by MariaDB is from -838:59:59 to 838:59:59.

There are some other functions that are related to the MAKETIME() function in Mariadb. They are:

  • MAKEDATE(): This function returns a date value that is composed of a given year and a given day of the year.
  • TIME(): This function returns the time part of a time or datetime value.
  • HOUR(): This function returns the hour part of a time or datetime value.
  • MINUTE(): This function returns the minute part of a time or datetime value.
  • SECOND(): This function returns the second part of a time or datetime value.
  • SEC_TO_TIME(): This function returns a time value that is composed of a given number of seconds.

Here are some examples of using these related functions:

-- Get a date value that is composed of the year 2020 and the day 100
SELECT MAKEDATE(2020, 100);

-- Get the time part of the datetime value '2020-04-09 12:34:56'
SELECT TIME('2020-04-09 12:34:56');

-- Get the hour part of the time value '12:34:56'
SELECT HOUR('12:34:56');

-- Get the minute part of the time value '12:34:56'
SELECT MINUTE('12:34:56');

-- Get the second part of the time value '12:34:56'
SELECT SECOND('12:34:56');

-- Get a time value that is composed of 3600 seconds
SELECT SEC_TO_TIME(3600);

Conclusion

In this article, we have learned how the MAKETIME() function works in Mariadb. We have seen its syntax, examples, and related functions. We have also learned how to use the MAKETIME() function to return a time value that is composed of a given hour, minute, and second. The MAKETIME() function is a useful function to create a time value from a given hour, minute, and second.