MySQL SEC_TO_TIME() Function

The SEC_TO_TIME() function in MySQL converts a given number of seconds into a time value (in HH:MM:SS format). This is useful when you need to work with time durations expressed as total seconds.



SEC_TO_TIME(): Definition and Usage

SEC_TO_TIME() takes an integer representing seconds as input and returns the equivalent time. This makes it easier to work with time durations that might be stored or calculated as total seconds. Both positive and negative values are allowed. The output will be in HH:MM:SS format.

Syntax

Syntax

SEC_TO_TIME(seconds)
      

Parameter Values

Parameter Description
seconds The number of seconds to convert to a time value. This is required and must be an integer. Can be positive or negative.

Examples

Converting Seconds to HH:MM:SS

This example converts 1 second to a time value.

Syntax

SELECT SEC_TO_TIME(1);
      
Output

00:00:01
      

Converting Negative Seconds to HH:MM:SS

This example demonstrates handling negative seconds.

Syntax

SELECT SEC_TO_TIME(-6897);
      
Output

-01:58:17
      

**Note:** The output for negative seconds will show a negative time value. If you input a number that represents more than 838 hours (the upper limit of the `TIME` datatype), the function will return `NULL`.