0
0
SQLquery~10 mins

YEAR, MONTH, DAY extraction in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - YEAR, MONTH, DAY extraction
Start with a date value
Apply YEAR() function
Extract year part
Apply MONTH() function
Extract month part
Apply DAY() function
Extract day part
Use extracted parts in query or output
Extract year, month, and day parts from a date using SQL functions YEAR(), MONTH(), and DAY() step-by-step.
Execution Sample
SQL
SELECT
  YEAR('2024-06-15') AS year_part,
  MONTH('2024-06-15') AS month_part,
  DAY('2024-06-15') AS day_part;
Extracts year, month, and day from the date '2024-06-15'.
Execution Table
StepFunction AppliedInput DateExtracted ValueOutput Column
1YEAR()'2024-06-15'2024year_part
2MONTH()'2024-06-15'6month_part
3DAY()'2024-06-15'15day_part
4Final Output-2024, 6, 15All columns
💡 All parts extracted; query returns one row with year=2024, month=6, day=15.
Variable Tracker
VariableStartAfter YEAR()After MONTH()After DAY()Final
year_partNULL2024202420242024
month_partNULLNULL666
day_partNULLNULLNULL1515
Key Moments - 3 Insights
Why does YEAR() return 2024 instead of the full date?
YEAR() extracts only the year part from the date string, ignoring month and day, as shown in execution_table step 1.
Can MONTH() return a value greater than 12?
No, MONTH() returns the month number (1-12) from the date, as shown in execution_table step 2 where it returns 6.
What happens if the input date is NULL?
All extraction functions return NULL if input is NULL, so no year, month, or day is extracted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does DAY() extract at step 3?
A6
B15
C2024
DNULL
💡 Hint
Check the 'Extracted Value' column at step 3 in execution_table.
At which step does the query extract the month from the date?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for the row where 'Function Applied' is MONTH() in execution_table.
If the input date was '2023-12-31', what would YEAR() return?
A31
B12
C2023
DNULL
💡 Hint
YEAR() extracts the year part; see variable_tracker for how year_part changes.
Concept Snapshot
YEAR(date) extracts the year number from a date.
MONTH(date) extracts the month number (1-12).
DAY(date) extracts the day number (1-31).
Use these functions to break down dates into parts.
They return integers representing each part.
If input is NULL, output is NULL.
Full Transcript
This visual execution shows how SQL extracts year, month, and day from a date string. Starting with the date '2024-06-15', the YEAR() function extracts 2024, MONTH() extracts 6, and DAY() extracts 15. Each step applies one function and outputs the corresponding part. Variables year_part, month_part, and day_part update as each function runs. The process stops after all parts are extracted, returning a single row with these values. Common confusions include understanding that YEAR() returns only the year, MONTH() never exceeds 12, and all functions return NULL if input is NULL. The quiz tests understanding of these extraction steps and outputs.