Mysql – Next quarter without using Quarter function

MySQL

I have a query which runs on particular dates. Whenever the query runs, it has to pick up the next quarter dates.

  • 1st quarter Jan-March
  • 2nd quarter April-June
  • 3rd quarter July-September
  • 4th quarter October- December

Suppose my query runs in April. I need to pick the data from July 1st to September 30th, without using the MySQL QUARTER function.

Best Answer

mysql> SELECT CONCAT_WS('-', YEAR(NOW()), MONTH(NOW()), '01') + INTERVAL
       3 * QUARTER(NOW()) + 1 - MONTH(NOW())
       MONTH;
--> 2016-07-01 

If you don't need the date of the beginning of the next quarter, but need the string '3rd quarter July-September', then use a CASE clause.

Without QUARTER:

SELECT CONCAT_WS('-', YEAR(NOW()), MONTH(NOW()), '01') + INTERVAL
       3 * FLOOR(MONTH(NOW())/3+1) + 1 - MONTH(NOW())
       MONTH;

Because QUARTER(x) is the same as FLOOR(MONTH(x)/3+1).