MySQL DATE()

The DATE() function extracts the date part of a date or datetime expression, returning it in 'YYYY-MM-DD' format. It is useful for working with only the date component of a timestamp.

Examples

1. Extracting the Date from a Datetime

SELECT DATE('2024-11-03 14:30:00') AS extracted_date;

Code Explanation: This query extracts the date part '2024-11-03' from the given datetime.

2. Using DATE() in a WHERE Clause

SELECT * FROM tamil_kings_events WHERE DATE(event_date) = '2024-11-03';

Code Explanation: This query filters records where the date part of event_date matches '2024-11-03'.

Best Practices

  • Use DATE() to extract the date part when working with datetime values.
  • Combine with other date functions for more complex date operations.

Key Takeaways

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