Anne Buena
Anne Buena

Reputation: 25

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);

Upvotes: 1

Views: 1447

Answers (6)

Amit
Amit

Reputation: 2585

Here is the code that you want:

select item from itemTable
where date_added >= ADDDATE(NOW(), INTERVAL - 14 DAY);

Upvotes: 0

Alex
Alex

Reputation: 11579

Try this (for MySQL):

SELECT * FROM table WHERE DATE_ADD(date_added, INTERVAL 2 WEEK) >= NOW();

Upvotes: 0

Yaroslav
Yaroslav

Reputation: 6554

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();

Upvotes: 1

Slimer
Slimer

Reputation: 1163

Basically it depends on database, but you can try something like this:

select * from table where date_added > getdate()-14

Upvotes: 0

Ashutosh Arya
Ashutosh Arya

Reputation: 1168

Simplest would be.

Where [Yourdatecolumn]<=GETDATE() AND [Yourdatecolumn]<=DATEADD(DD,-13,GETDATE())

Another option would be

WHERE DATEDIFF(DD, [Yourdatecolumn],GETDATE()) IS BETWEEN 0 and 13

Upvotes: 0

CrckrJack
CrckrJack

Reputation: 93

Give this a go:

SELECT *
FROM yourTable
WHERE Date_Added >= DATEADD( week, -2, GETDATE())

Upvotes: 0

Related Questions