MySQL TIME() Function
MySQL TIME() Function
The MySQL TIME() function extracts the time portion from a TIME or DATETIME expression and returns it as a TIME value. For example, '2026-05-10 14:35:20' becomes '14:35:20'.
Developers use TIME() to isolate the clock time when comparing store opening hours, checking afternoon order spikes across multiple days, or stripping calendar dates before display.
If the input includes fractional seconds, TIME() preserves them in the returned TIME value.
Syntax
TIME(expr)
expr: ATIME,DATETIME, orTIMESTAMPexpression, or a recognized time/datetime string.
Practical Example
In demo_orders, created_at stores full DATETIME values. You can extract the time component to analyze orders placed after 5:00 PM regardless of the calendar date.
SELECT
order_id,
created_at,
TIME(created_at) AS order_time
FROM demo_orders
WHERE TIME(created_at) >= '17:00:00'
ORDER BY order_time;
| order_id | created_at | order_time |
|---|---|---|
| 1012 | 2026-05-08 17:05:11 | 17:05:11 |
| 1019 | 2026-05-10 19:42:30 | 19:42:30 |
| 1024 | 2026-05-09 22:18:04 | 22:18:04 |
Each row displays only its hh:mm:ss portion, making time-of-day sorting straightforward.
Common Mistakes & NULL Behavior
Passing a date-only string like TIME('2026-05-10') is a common mistake. Without a time component, MySQL tries to parse '2026-05-10' as a time literal, fails because 2026 hours exceeds the 838 hour limit, emits a warning, and returns NULL. Always pass a valid DATETIME or TIME value.
Similarly, wrapping an indexed DATETIME column in WHERE TIME(created_at) >= '17:00:00' disables standard B-tree index range lookups on created_at. On large tables, filter by a date range first or store a generated indexed TIME column.
TIME() returns NULL if expr is NULL, contains an invalid calendar date like '2026-02-30 10:00:00', or contains invalid clock digits such as '12:75:00'.
Frequently Asked Questions
Does TIME() preserve fractional seconds from DATETIME(6)?
Yes. Calling TIME('2026-05-10 14:20:05.123456') returns '14:20:05.123456'.
Can TIME() return values greater than 23:59:59?
When expr is already a TIME duration like '36:15:00' or '-12:00:00', TIME(expr) returns that duration unchanged within -838:59:59 to 838:59:59.
How do I extract only the date instead of the time?
Use the DATE(expr) function to return the 'YYYY-MM-DD' portion of a DATETIME value.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.