Select the first and last day of the previous month

2

I have the following query :

select  
ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1) primeiro_dia, 
last_day(sysdate()) ultimo_dia

That returns me the first and last day of the current month according to the system date. How could I return the first and last day of the previous month according to the system date too?

    
asked by anonymous 04.06.2018 / 14:29

2 answers

3

Given the query you already have, just fetch the same data by subtracting from the date a month:

select
   ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1) primeiro_dia, 
   last_day(sysdate()) ultimo_dia,
   ADDDATE(LAST_DAY(SUBDATE(DATE_SUB(curdate(), INTERVAL 1 MONTH), INTERVAL 1 MONTH)), 1) primeiro_dia_mes_passado, 
   last_day(DATE_SUB(curdate(), INTERVAL 1 MONTH)) ultimo_dia_mes_passado
    
04.06.2018 / 14:33
0
SELECT 
   DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY) primeiro_dia_mes_anterior,
   (LAST_DAY(NOW() - INTERVAL 1 MONTH)) ultimo_dia_mes_anterior
    
04.06.2018 / 14:59