What is DAYOFYEAR() in SQL?

The DAYOFYEAR() function returns the day of the year from a date sent as a parameter. There are 365 days in a year.

There are 366 days in a leap year.

Figure 1 shows a visual representation of the DAYOFYEAR() function.

Figure 1: Visual representation of DAYOFYEAR() function

Syntax

DAYOFYEAR(date)

Parameter

The DAYOFYEAR() function takes the date as a parameter.

Dates must be in the format YYYY-MM-DD, or else this function returns NULL.

Return value

The DAYOFYEAR() function returns the day of the year from a date sent as a parameter.

It returns NULL if the date is invalid.

Example

-- current date
SELECT DAYOFYEAR(NOW());
-- invalid month
SELECT DAYOFYEAR('2021-13-12');
-- valid month/day
SELECT DAYOFYEAR('2021-05-03');
-- invalid day
SELECT DAYOFYEAR('2021-2-30');

Free Resources