Select that takes the last 30 days from the current date

Asked

Viewed 9,612 times

2

Good morning guys, this is a part of query which I used to search the last days from the current date. It works as expected when I put -20, but by putting -30, nay.

WHERE viacao_os.creation_date BETWEEN CURRENT_DATE() -30 AND CURRENT_DATE()

How can I consult what was created in the last 30 days?

2 answers

5


Use the DATE_ADD():

WHERE viacao_os.creation_date
    BETWEEN DATE_ADD(CURRENT_DATE(), INTERVAL -30 DAY) AND CURRENT_DATE()

Possibly your query works with the -20 for being on the day 21, that is, chance.

  • WHERE viacao_os.creation_date BETWEEN DATE_ADD(CURRENT_DATE() INTERVAL -30 DAY) AND CURRENT_DATE() Order by viacao_os.os_id

  • 1

    Made that mistake

  • @user107357, missing a comma in the date_add. Fix it, try again.

  • Presented this error You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near 'INTERVAL -30 DAY) AND CURRENT_DATE() Order by viacao_os.os_id

  • 1

    Thank you, it worked right, very grateful.

  • mass :) just remember to mark the answer as accepted ;)

Show 1 more comment

-2

To show the last 30 days, use:

select 
    to_date(to_char(sysdate-30,'dd/mm/yyyy'), 'dd/mm/yyyy') + rownum -1 AS data
from 
    all_objects
where 
    rownum <= to_date(to_char(sysdate,'dd/mm/yyyy'),'dd/mm/yyyy') - to_date(to_char(sysdate-30,'dd/mm/yyyy'),'dd/mm/yyyy')+1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.