In MySQL, processing dates and times is a common task. Usually, we need to extract some information such as year, month, day, etc. from known dates. MySQL provides some built-in functions that can be easily performed. This article will explain in detail how to obtain the year, month, and day parts of known dates separately in MySQL.
1. Extraction year (Year)
To extract the year from the date, you can useYEAR()
Function. This function takes a date parameter and returns the year part of the date.
Example
Suppose we have a name calledevents
table, containing a nameevent_date
date column. We can extract it through the following queryevent_date
Years in:
SELECT event_date, YEAR(event_date) AS year FROM events;
Output result
event_date | year |
---|---|
2023-07-01 | 2023 |
2024-01-15 | 2024 |
2. Extract month (Month)
To extract months from dates, you can useMONTH()
Function. This function takes a date parameter and returns the month part of the date (1 to 12).
Example
Similarly, fromevent_date
Extracted months:
SELECT event_date, MONTH(event_date) AS month FROM events;
Output result
event_date | month |
---|---|
2023-07-01 | 7 |
2024-01-15 | 1 |
3. Extraction date (Day)
To extract the date part from the date, you can useDAY()
Function. This function takes a date parameter and returns the number of days (1 to 31) of that date.
Example
fromevent_date
Extract date part:
SELECT event_date, DAY(event_date) AS day FROM events;
Output result
event_date | day |
---|---|
2023-07-01 | 1 |
2024-01-15 | 15 |
IV. Comprehensive examples
We can combine the above functions to extract year, month and day information from dates at the same time.
Example
fromevent_date
Extract year, month, and date:
SELECT event_date, YEAR(event_date) AS year, MONTH(event_date) AS month, DAY(event_date) AS day FROM events;
Output result
event_date | year | month | day |
---|---|---|---|
2023-07-01 | 2023 | 7 | 1 |
2024-01-15 | 2024 | 1 | 15 |
V. Other date and time functions
Extract hours, minutes, seconds
In addition to year, month, and day, MySQL also provides functions to extract the time part, such asHOUR()
、MINUTE()
andSECOND()
。
Example
Suppose there is a name calledlog_time
We can extract hours, minutes and seconds respectively for timestamp columns:
SELECT log_time, HOUR(log_time) AS hour, MINUTE(log_time) AS minute, SECOND(log_time) AS second FROM logs;
Output result
log_time | hour | minute | second |
---|---|---|---|
2023-07-01 12:34:56 | 12 | 34 | 56 |
2024-01-15 23:45:01 | 23 | 45 | 1 |
Extract the weekday
Can be usedWEEKDAY()
Function extracts the day of the week, from 0 (Monday) to 6 (Sunday).
Example
fromevent_date
Extract the day of the week:
SELECT event_date, WEEKDAY(event_date) AS weekday FROM events;
Output result
event_date | weekday |
---|---|
2023-07-01 | 5 |
2024-01-15 | 0 |
6. Use the DATE_FORMAT function
DATE_FORMAT()
Functions allow formatting dates based on specified format strings. In this way, various parts of the date can be flexibly extracted.
Example
fromevent_date
Extract year, month, and date:
SELECT event_date, DATE_FORMAT(event_date, '%Y') AS year, DATE_FORMAT(event_date, '%m') AS month, DATE_FORMAT(event_date, '%d') AS day FROM events;
Output result
event_date | year | month | day |
---|---|---|---|
2023-07-01 | 2023 | 07 | 01 |
2024-01-15 | 2024 | 01 | 15 |
This is the end of this article about obtaining the year, month and date of known dates in MySQL. For more related MySQL to obtain the year, month and date, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!