skip to Main Content

I have this date format :
YYYY-MM-DD (example : 2022-05-10)
I want to extract only last 6 months inclunding current months.
The result normaly it’s : june, july,august, september, octobre, november (6 last months)

I do this request :

Select created_date
from table_A
Where created_date >= now() - INTERVAL 6 MONTH

The query gives me the last 7 months, that is to say from May 2022 to November 2022 and this is not what I want.

I want the last 6 months including the current month, i.e. from June to November

thank you in advance for your help

2

Answers


  1. Today is 2022-11-21 and you want everything on or after 2022-06-01?

    That is beginning of current month (2022-11-01) – 5 months:

    SELECT created_date
    FROM table_A
    WHERE created_date >= (CURRENT_DATE - INTERVAL (DAYOFMONTH(CURRENT_DATE) - 1) DAY) - INTERVAL 5 MONTH;
    
    Login or Signup to reply.
  2. Simplest way to do this is:

    WHERE created_date >= last_day(current_date()) + interval 1 day - interval 6 month
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search