MySQL DATE_FORMAT()
The DATE_FORMAT()
function formats a date according to a specified format string. It is useful for presenting date values in a more readable format.
Examples
1. Formatting a Date
SELECT DATE_FORMAT('2024-11-03', '%W, %M %d, %Y') AS formatted_date;
Code Explanation: This query formats the date '2024-11-03' as 'Sunday, November 03, 2024'.
2. Formatting Dates in a Table
SELECT event_name, DATE_FORMAT(event_date, '%D %b %Y') AS formatted_date FROM tamil_kings_events;
Code Explanation: This query formats the event_date
in 'Day Month Year' format for all events.
Best Practices
- Use
DATE_FORMAT()
to format dates for display purposes. - Refer to the MySQL documentation for format specifiers to customize the output.
Key Takeaways
- The
DATE_FORMAT()
function formats date values according to a specified format. - It is useful for making dates more readable and user-friendly.