Extract Date Parts in MySQL: Year, Month, Day

One way to extract the year, month, and day from a date/time field is by using the DATE_FORMAT function in MySQL. Here is an example query that demonstrates how to extract the year, month, and day from a field named datetime_field.

SELECT DATE_FORMAT(datetime_field, '%Y-%m-%d') AS date_only
FROM your_table;

In the above query, datetime_field is the name of the field containing date and time values, and your_table is the name of the table. The first parameter of the DATE_FORMAT function is the date and time field to be formatted, while the second parameter is the date format string. In this example, ‘%Y-%m-%d’ specifies the output date format as year-month-day. You can adjust the date format string as needed to obtain different date formats.

bannerAds