Lesson

MySQL SECOND() Function

10 min reading
Free Course

MySQL SECOND() Function

The MySQL SECOND() function extracts the whole-seconds component from a TIME, DATETIME, or TIMESTAMP expression. It returns an integer in the range 0 to 59.

Developers use SECOND() when analyzing server logs, validating scheduled cron execution offsets, or inspecting timing jitter in job queues. It strips away hours, minutes, and fractional microseconds.

Even when a timestamp includes fractional digits like .999999, SECOND() truncates rather than rounds. For example, '14:20:09.950000' returns 9, not 10.

Syntax

SECOND(time)
  • time: A TIME, DATETIME, or TIMESTAMP value, or valid time literal.

Practical Example

Suppose demo_orders tracks payment gateway completion in paid_at. You can inspect both SECOND() and MICROSECOND() to check how closely automated retries align to exact second boundaries.

SELECT
    order_id,
    paid_at,
    SECOND(paid_at) AS paid_second,
    MICROSECOND(paid_at) AS paid_microsecond
FROM demo_orders
WHERE paid_at >= '2026-05-10 10:00:00'
  AND paid_at < '2026-05-10 10:05:00';
order_id paid_at paid_second paid_microsecond
501 2026-05-10 10:01:05.820000 5 820000
502 2026-05-10 10:02:49.000000 49 0
503 2026-05-10 10:04:00.150000 0 150000

Notice order 501: even with .820000 fractional seconds, SECOND() returns 5.

Common Mistakes & NULL Behavior

Do not confuse SECOND() with TIME_TO_SEC(). SECOND('01:02:03') returns only 3, whereas TIME_TO_SEC('01:02:03') converts the full duration into 3723 total seconds.

Also watch negative durations. Calling SECOND('-00:05:42') returns positive 42, not -42.

SECOND() returns NULL if time is NULL or cannot be parsed as a valid time value (for example, '10:20:75', where seconds exceed 59). Passing a DATE value converted from a date column treats the missing time as 00:00:00 and returns 0, whereas an unrecognized string returns NULL.

Frequently Asked Questions

Does SECOND() round up when microseconds are >= 500000?

No. SECOND('12:00:15.900000') returns 15. Combine SECOND() and MICROSECOND() or round the timestamp first if rounding is needed.

How do I get total seconds since midnight for a time?

Use TIME_TO_SEC(time_col) instead of SECOND(time_col).

Can SECOND() return 60 for leap seconds?

No. MySQL TIME and DATETIME types restrict the seconds field to 0 through 59.

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