w3resource

MySQL Date and Time Exercises: Get the first day of the current year

MySQL Date Time: Exercise-4 with Solution

Write a MySQL query to get the first day of the current year.

Code:

-- This SQL query creates a date using the year extracted from the current date and the day '1'.

SELECT 
MAKEDATE( -- Creates a date using the specified year and day.
EXTRACT(YEAR FROM CURDATE()), -- Extracts the year from the current date (CURDATE()).
        1 -- Specifies the day as '1'.
    );

Explanation:

  • The EXTRACT(YEAR FROM CURDATE()) function extracts the year from the current date using the CURDATE() function.
  • The MAKEDATE() function then takes the extracted year and the day '1' to create a date value.
  • This effectively creates a date for the first day of the current year.

Sample Output:

MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1)
2017-01-01T05:00:00.000Z

 

MySQL Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a MySQL query to get the distinct Mondays from hire_date in employees tables.
Next:Write a MySQL query to get the last day of the current year.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql-exercises/date-time-exercises/write-a-query-to-get-the-first-day-of-the-current-year.php