MySQL DAYOFYEAR() function; In this tutorial, i am going to show you MySQL DAYOFYEAR function with the help of examples.
MySQL DAYOFYEAR() Function
MySQL DAYOFYEAR function is one of the MySQL Date Functions, which returns the day number of the year from the given date.
Syntax of MySQL DAYOFYEAR() Function
Basic Syntax of DAYOFYEAR() function is:
DAYOFYEAR(date)
The date here is the date value that you want the day of the year from which it was returned.
Example 1 – MySQL DAYOFYEAR() Function
Let’s take an example using mysql dayofyear() function; as follows:
SELECT DAYOFYEAR('2020-06-18') AS 'Result';
Output-1
+--------+ | Result | +--------+ | 170 | +--------+
Example 2 – MySQL DAYOFYEAR() Function
Let’s take an second example using mysql dayofyear() function; as follows:
SELECT DAYOFYEAR('2018-02-01') AS 'Result';
Output-2
+--------+ | Result | +--------+ | 32 | +--------+
Example 3 – MySQL DAYOFYEAR() Function
To records from the database at that time we want to remove the day of the year from create_at(or any date column of your table); as follows:
SELECT created_at AS create_date, DAYOFYEAR(created_at) AS day_of_year FROM users WHERE id= 112;
Output-3
+---------------------+--------------+ | create_date | day_of_year | +---------------------+--------------+ | 2010-08-23 10:33:39 | 235 | +---------------------+--------------+
Example 4 – MySQL DAYOFYEAR() with Now() Function
To extract the day of the year from the current date and time (which is now returned using the () function); as follows:
SELECT NOW(), DAYOFYEAR(NOW());
Output-4
+---------------------+--------------------+ | NOW() | DAYOFYEAR(NOW()) | +---------------------+--------------------+ | 2019-07-12 18:30:44 | 193 | +---------------------+--------------------+
Example 5 – MySQL DAYOFYEAR() Function
Let’s take the next example of using CURDATE () function with dayofyear(); as follows:
SELECT CURDATE(), DAYOFYEAR(CURDATE());
Output-5
+------------+-----------------------+ | CURDATE() | DAYOFYEAR(CURDATE()) | +------------+-----------------------+ | 2019-07-12 | 193 | +------------+-----------------------+
Conclusion
MySQL DAYOFYEAR() function tutorial, You have learned how to use mysql DAYOFYEAR() function with various examples.