Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL Date and Time Exercises: Display the first day of the month three months before the current month

MySQL Date Time: Exercise-1 with Solution

Write a query to display the first day of the month (in datetime format) three months before the current month.

Sample current date : 2014-09-03
Expected result : 2014-06-01

Code:

SELECT date(((PERIOD_ADD
  (EXTRACT(YEAR_MONTH 
     FROM CURDATE()),-3)*100)+1));

Sample Output:

date(((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-3)*100)+1))
2017-05-01T04:00:00.000Z

 

MySQL Code Editor:

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

Previous:Date and Time functions
Next:Write a query to display the last day of the month (in datetime format) three months before the current month.

What is the difficulty level of this exercise?