• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

SQL query to return records from the past X number of days

Im trying to write an SQL query to return records from the past X number of days
I saw the following  answer but when  writing this query (directly to the database in phphmyadmin)
its say DATEADD doesnt exist
select * from myTable where OrderDate >= DATEADD(d, -30, getdate())
0
Nura111
Asked:
Nura111
  • 2
1 Solution
 
Philippe DamervalSenior Analyst ProgrammerCommented:
Hi,
DATEADD in "mysqlese" is ADDDATE :) Sorry, didn't make it up.
you can review and learn about MySQL date and time  functions here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
0
 
Philippe DamervalSenior Analyst ProgrammerCommented:
... assign  points?
0
 
K VDatabase ConsultantCommented:
In mysql we have DATE_ADD and DATE_SUB, you should use DATE_SUB.

select * from myTable where OrderDate >= DATE_SUB(d, interval 30 days);

Also, date_sub will return datetime; if you want to compare date you will need:

select * from myTable where OrderDate >= DATE(DATE_SUB(d, interval 30 days));
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now