How SEC_TO_TIME() Works in MariaDB

In MariaDB, SEC_TO_TIME() is a built-in date and time function that returns a time value, based on the number of seconds provided as arguments.

Syntax

The syntax goes like this:

SEC_TO_TIME(seconds)

Example

Here’s an example:

SELECT SEC_TO_TIME(1);

Result:

+----------------+
| SEC_TO_TIME(1) |
+----------------+
| 00:00:01       |
+----------------+

Here’s another one:

SELECT SEC_TO_TIME(18520);

Result:

+--------------------+
| SEC_TO_TIME(18520) |
+--------------------+
| 05:08:40           |
+--------------------+

Out of Range Seconds

The range of the result is constrained to that of the time data type. A warning occurs if the argument corresponds to a value outside that range. The range for MariaDB time values is '-838:59:59.999999' to '838:59:59.999999'.

Anyway, here’s an example of a time with values that are on the upper end of their accepted range:

SELECT SEC_TO_TIME(3020399);

Result:

+----------------------+
| SEC_TO_TIME(3020399) |
+----------------------+
| 838:59:59            |
+----------------------+

And here’s one that goes outside the range:

SELECT SEC_TO_TIME(3020400);

Result:

+----------------------+
| SEC_TO_TIME(3020400) |
+----------------------+
| 838:59:59            |
+----------------------+
1 row in set, 1 warning (0.000 sec)

Here’s the warning:

SHOW WARNINGS;

Result:

+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect seconds value: '3020400' |
+---------+------+----------------------------------------------+

Microseconds

We can go even closer to the upper range by including microseconds:

SELECT SEC_TO_TIME(3020399.999999);

Result:

+-----------------------------+
| SEC_TO_TIME(3020399.999999) |
+-----------------------------+
| 838:59:59.999999            |
+-----------------------------+

Negative Arguments

Providing a negative amount results in a negative time value.

Example:

SELECT SEC_TO_TIME(-3020399);

Result:

+-----------------------+
| SEC_TO_TIME(-3020399) |
+-----------------------+
| -838:59:59            |
+-----------------------+

Missing Argument

Calling SEC_TO_TIME() with the wrong number of arguments, or without passing any arguments, results in an error:

SELECT SEC_TO_TIME();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SEC_TO_TIME'

And another example:

SELECT SEC_TO_TIME( 123, 456 );

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SEC_TO_TIME'

Make a Time

Also see MAKETIME() for constructing a time value from its hours, minutes, and seconds.