MySQL TIME()

The TIME() function extracts the time part of a datetime expression, returning it in 'HH:MM:SS' format. It is useful for working with only the time component.

Examples

1. Extracting the Time from a Datetime

SELECT TIME('2024-11-03 14:30:00') AS extracted_time;

Code Explanation: This query extracts the time part '14:30:00' from the given datetime.

2. Using TIME() in a WHERE Clause

SELECT * FROM tamil_kings_events WHERE TIME(event_date) = '14:30:00';

Code Explanation: This query filters records where the time part of event_date matches '14:30:00'.

Best Practices

  • Use TIME() to extract the time part when working with datetime values.
  • Be consistent with time formats when comparing or filtering.

Key Takeaways

  • The TIME() function extracts the time component of a datetime value.
  • It is useful for comparisons and filtering by time.