-1

I am trying to grab all SQL entries WHERE Day difference between current date and expiration date is < than 4 days

My first approach was following:

$sql_i_requested = "SELECT *, (To_days(date_return)-TO_DAYS(NOW())) as daydif FROM ".$tbl_name." 
WHERE (status!='completed' AND status!='canceled') 
AND owner_id=".$owner_id." 
AND daydif < 4 
ORDER BY date_created DESC";

My second aproach is (according to SQL DateDifference in a where clause):

$sql_i_requested = "SELECT * FROM ".$tbl_name." 
WHERE (status!='completed' AND status!='canceled') 
AND owner_id=".$owner_id." 
AND date_return > DateAdd(day, -3, getdate())
ORDER BY date_created DESC";

Neither of them work, so how do I select FROM table WHERE day_difference between "date_return" and now() is less than 4 days?

EDIT:

changed

AND daydif < 4 

to

AND (To_days(date_return)-TO_DAYS(NOW())) < 4 

and now it's working. Anyway, maybe you guys could suggest other solutions.

Community
  • 1
  • 1
Alex
  • 4,607
  • 9
  • 61
  • 99

3 Answers3

0

try:

SELECT DATEDIFF(date_return, NOW()) AS dayDiff;

and

having dayDiff < 4
GGio
  • 7,563
  • 11
  • 44
  • 81
0

Try this:

select * 
from  table
where DATEDIFF(day, present, future) < 4
EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59
0

Using the DATEDIFF

WHERE DATEDIFF(date_return, now()) < 4
Muhammad Hani
  • 8,476
  • 5
  • 29
  • 44
  • What's better (faster) in terms of performance? `DATEDIFF(date_return, now())` or `(To_days(date_return)-TO_DAYS(NOW()))` ? – Alex Feb 20 '13 at 17:42