MySQL DAYOFYEAR() Function
MySQL DAYOFYEAR(): Find the Ordinal Day Within a Year
The DAYOFYEAR() function returns the day number of a date within its calendar year, ranging from 1 (January 1) to 365 in common years or 366 in leap years.
Finance and sales teams use DAYOFYEAR() for year-to-date pacing comparisons and seasonal trend analysis.
Syntax
Supply one date or timestamp expression:
DAYOFYEAR(date_value)
date_value: ADATE,DATETIME,TIMESTAMP, or valid date string.
The output is an integer from 1 to 366. Invalid dates evaluate to NULL.
Practical Example
Suppose your sales team compares year-to-date order volume in demo_orders up to the current day of the year.
SELECT
YEAR(order_date) AS sales_year,
COUNT(*) AS ytd_orders,
DAYOFYEAR(MAX(order_date)) AS latest_day_number
FROM demo_orders
WHERE DAYOFYEAR(order_date) <= DAYOFYEAR('2026-05-12')
GROUP BY YEAR(order_date);
| sales_year | ytd_orders | latest_day_number |
|---|---|---|
| 2025 | 142 | 132 |
| 2026 | 168 | 132 |
Because 2026-05-12 is day 132 of the year, filtering DAYOFYEAR(order_date) <= 132 pulls matching year-to-date windows across multiple years.
Common Mistakes & NULL Behavior
Watch out for these subtle issues with DAYOFYEAR():
- Leap year day shift: In a leap year like 2024, March 1 is day
61, whereas in 2025 or 2026, March 1 is day60. Year-over-yearDAYOFYEAR()comparisons shift by one day after February 28. NULLand invalid dates returnNULL: CallingDAYOFYEAR(NULL),DAYOFYEAR('0000-00-00'), orDAYOFYEAR('2026-02-30')returnsNULL.- Multi-year grouping bug: Grouping by
DAYOFYEAR(order_date)alone merges January 1, 2025 and January 1, 2026 into the same bucket. Always includeYEAR(order_date)if separating years. - No
TIME-only input: Passing'09:30:00'without a date returnsNULL.
Frequently Asked Questions
What does DAYOFYEAR() return for December 31?
It returns 365 for regular years like 2026-12-31 and 366 for leap years like 2024-12-31.
How do I convert a year and day-of-year number back to a DATE?
Use MAKEDATE(year, dayofyear). For example, MAKEDATE(2026, 132) returns '2026-05-12'.
Does DAYOFYEAR() work on DATETIME columns?
Yes. DAYOFYEAR() reads only the date portion of a DATETIME or TIMESTAMP and ignores hours, minutes, and seconds.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.