Skip to content
Advertisement

SQL Query Comparing Date

I have a table of items with a ‘date_added’ column. What I want to do is select all the items added during the last two weeks. How can I do that?

 $sql = "SELECT *
           FROM iteminfo
          WHERE quantity > 0
          ORDER BY ID ASC";
 // $query = mysql_query($sql);

Advertisement

Answer

If you are using MS SQL Server try this code:

SELECT tb.date_added
  FROM MyTable tb
 WHERE tb.date_added > DATEADD(week, -2, GETDATE())

For MySQL try:

SELECT tb.date_added
  FROM MyTable tb
 WHERE DATE_ADD(tb.date_added, INTERVAL 2 WEEK) >= NOW();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement