MySQL DAYOFYEAR() Function
In MySQL, the DAYOFYEAR()
function returns a number from 1
to 366
representing the day of the year.
DAYOFYEAR()
Syntax
Here is the syntax of MySQL DAYOFYEAR()
function:
DAYOFYEAR(expr)
Parameters
expr
- Required. A date or datetime expression.
Return value
The MySQL DAYOFYEAR()
function returns a number from 1
to 366
representing the day of the year.
If the specified expression is not a valid date or datetime, the DAYOFYEAR()
function will return NULL
.
If the argument is NULL
, the DAYOFYEAR()
function will return NULL
.
DAYOFYEAR()
Examples
Here are some examples of the DAYOFYEAR()
function.
SELECT
DAYOFYEAR('2022-01-01'),
DAYOFYEAR('2022-01-31'),
DAYOFYEAR('2022-02-28 10:10:10'),
DAYOFYEAR('2022-02-00'),
DAYOFYEAR('2022-02-30'),
DAYOFYEAR('Not A DATE'),
DAYOFYEAR(NULL)\G
DAYOFYEAR('2022-01-01'): 1
DAYOFYEAR('2022-01-31'): 31
DAYOFYEAR('2022-02-28 10:10:10'): 59
DAYOFYEAR('2022-02-00'): NULL
DAYOFYEAR('2022-02-30'): NULL
DAYOFYEAR('Not A DATE'): NULL
DAYOFYEAR(NULL): NULL
To return day of the year for today, you can use NOW()
, or CURDATE()
, or CURRENT_DATE()
, or SYSDATE()
. For example:
SELECT
DAYOFYEAR(NOW()),
DAYOFYEAR(CURDATE()),
DAYOFYEAR(CURRENT_DATE()),
DAYOFYEAR(SYSDATE())\G
DAYOFYEAR(NOW()): 103
DAYOFYEAR(CURDATE()): 103
DAYOFYEAR(CURRENT_DATE()): 103
DAYOFYEAR(SYSDATE()): 103
Note that your results may be different.