Lesson

MySQL SEC_TO_TIME() Function

10 min reading
Free Course

MySQL SEC_TO_TIME() Function

The MySQL SEC_TO_TIME() function converts a numeric count of seconds into a TIME value formatted as 'hh:mm:ss' (or hhmmss in numeric contexts). It is the direct inverse of TIME_TO_SEC().

Applications often store call durations, video lengths, or task runtimes as integer seconds for easy SUM() and AVG() math. When presenting those totals in reports, SEC_TO_TIME() turns raw seconds back into readable hours, minutes, and seconds.

SEC_TO_TIME() supports both negative values and fractional decimal seconds.

Syntax

SEC_TO_TIME(seconds)
  • seconds: An integer or decimal number representing seconds (positive, zero, or negative).

Practical Example

Suppose demo_employees logs average support call durations or shift lengths, or demo_orders tracks fulfillment time in seconds (fulfillment_seconds). You can convert those integers into HH:MM:SS durations.

SELECT
    order_id,
    fulfillment_seconds,
    SEC_TO_TIME(fulfillment_seconds) AS fulfillment_duration
FROM demo_orders
WHERE order_id IN (601, 602, 603);
order_id fulfillment_seconds fulfillment_duration
601 45 00:00:45
602 3725 01:02:05
603 93600 26:00:00

Notice order 603: 93600 seconds converts cleanly to 26:00:00 because MySQL TIME represents durations, not just 24-hour clock times.

Common Mistakes & NULL Behavior

The most critical limit to know is MySQL's TIME range: -838:59:59 to 838:59:59 (roughly 3,020,399 seconds, or 34.95 days). If you sum seconds across thousands of rows and exceed 3,020,399 seconds, SEC_TO_TIME() clamps the result to 838:59:59 and emits a truncation warning!

For large totals exceeding 838 hours, format days and hours manually using FLOOR(s / 86400) or TIMESTAMPDIFF() instead of SEC_TO_TIME().

Also avoid passing SEC_TO_TIME() inside TIME_FORMAT() when hours might exceed two digits or turn negative, unless you verify formatting behavior. SEC_TO_TIME() returns NULL when seconds is NULL.

Frequently Asked Questions

Can SEC_TO_TIME() handle negative seconds?

Yes. For example, SEC_TO_TIME(-3661) returns '-01:01:01'.

What happens if seconds exceed 838:59:59?

MySQL clamps the output to '838:59:59' (or '-838:59:59' for negative overflow) and generates a warning.

Does SEC_TO_TIME() keep fractional seconds?

Yes. Passing a decimal like SEC_TO_TIME(65.25) returns '00:01:05.25'.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum