Name

DAYOFYEAR( )

Synopsis

DAYOFYEAR(date)

This function returns the day of the year. January 1 would give a value of 1, and December 31 would normally be 365, except on leap years, when it would be 366.

SELECT (DAYOFYEAR('2004-03-01') -
       DAYOFYEAR('2004-02-28'))
          AS 'Difference for Leap Year';
+--------------------------+
| Difference for Leap Year |
+--------------------------+
|                        2 |
+--------------------------+

Here we are using the function to calculate the number of days from the first date, March 1, 2004, to the second date, February 28 of the same year. 2004 was a leap year, so the result is 2 days.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.