SQL Tutorial SQL Advanced SQL Database SQL References

MySQL DAYOFYEAR() Function



The MySQL DAYOFYEAR() function returns the day of the year of a given date or datetime value. It can be between 1 to 366.

Syntax

DAYOFYEAR(datetime)

Parameters

datetime Required. Specify a date or datetime value from which to extract the day of year.

Return Value

Returns the day of the year of a given date or datetime value.

Example 1:

The example below shows the usage of DAYOFYEAR() function.

mysql> SELECT DAYOFYEAR('2018-08-18');
Result: 230

mysql> SELECT DAYOFYEAR('2018-08-18 10:38:42');
Result: 230

mysql> SELECT DAYOFYEAR('2018-08-18 10:38:42.000004');
Result: 230

mysql> SELECT DAYOFYEAR('2014-10-20');
Result: 293

mysql> SELECT DAYOFYEAR(CURDATE());
Result: 315

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the day of the year of records of column OrderTime:

SELECT *, DAYOFYEAR(OrderTime) AS DAYOFYEAR_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeDAYOFYEAR_Value
1001.582017-08-18 10:38:42230
1201.612018-03-23 07:14:1682
1251.782018-09-12 05:25:56255
501.802019-01-16 11:52:0516
2001.722020-02-06 09:31:3437

❮ MySQL Functions