0
0
MySQLquery~10 mins

EXTRACT and YEAR/MONTH/DAY in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - EXTRACT and YEAR/MONTH/DAY
Start with a date/time value
Choose part to extract: YEAR, MONTH, or DAY
Use EXTRACT function
Return the chosen part as a number
Use result in query or display
The EXTRACT function takes a date/time and returns the year, month, or day part as a number.
Execution Sample
MySQL
SELECT EXTRACT(YEAR FROM '2024-06-15') AS year_part,
       EXTRACT(MONTH FROM '2024-06-15') AS month_part,
       EXTRACT(DAY FROM '2024-06-15') AS day_part;
This query extracts the year, month, and day parts from the date '2024-06-15'.
Execution Table
StepFunction CallInput DateExtracted PartResult
1EXTRACT(YEAR FROM '2024-06-15')2024-06-15YEAR2024
2EXTRACT(MONTH FROM '2024-06-15')2024-06-15MONTH6
3EXTRACT(DAY FROM '2024-06-15')2024-06-15DAY15
4Query completes--Returns one row with year=2024, month=6, day=15
💡 All parts extracted successfully; query returns one row with three columns.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
year_partNULL2024202420242024
month_partNULLNULL666
day_partNULLNULLNULL1515
Key Moments - 2 Insights
Why does EXTRACT return a number and not a date?
EXTRACT returns only the specified part (year, month, or day) as a number, not the full date. See execution_table rows 1-3 where each call returns a number.
Can EXTRACT be used on date strings directly?
Yes, as long as the string is in a valid date format like 'YYYY-MM-DD'. The example uses '2024-06-15' directly (execution_table input).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of EXTRACT(MONTH FROM '2024-06-15')?
A15
B2024
C6
DNULL
💡 Hint
Check execution_table row 2 under Result column.
At which step does the day part get extracted?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table rows and see which step extracts DAY.
If the input date was '2023-12-01', what would EXTRACT(YEAR FROM date) return?
A1
B2023
C12
DNULL
💡 Hint
EXTRACT(YEAR FROM ...) returns the year part as shown in variable_tracker and execution_table.
Concept Snapshot
EXTRACT(part FROM date) extracts a part of a date.
Common parts: YEAR, MONTH, DAY.
Returns a number representing that part.
Input date must be valid format.
Useful to get date components for filtering or display.
Full Transcript
This lesson shows how to use the EXTRACT function in MySQL to get the year, month, or day from a date. The function takes two inputs: the part you want (YEAR, MONTH, or DAY) and the date value. It returns the number for that part. For example, EXTRACT(YEAR FROM '2024-06-15') returns 2024. We traced each step where the year, month, and day are extracted separately. The variables year_part, month_part, and day_part hold these values after each step. This helps when you want to filter or display parts of dates in your database queries.