-1

I am trying to run a MySQL query but I am having trouble with DateDiff.

I have 2 date fields payment and expiry.

I want to select the Difference in dates between expiry and payment which are less than or equal to thirty five days.

How can I use datediff(expiry,payment) and then include <= 35 to get what I want.

Thanks.

StefanHanotin
  • 191
  • 5
  • 17
  • 1
    Can not help but wonder... what exactly have you tried before asking this question? – grr Jul 03 '12 at 10:18

2 Answers2

2

You can add a WHERE clause to your query;

SELECT cols,
       datediff(expiry, payment)

FROM   sometable

WHERE datediff(expiry, payment) <= 35

That should do it.

dash
  • 89,546
  • 4
  • 51
  • 71
  • Cheers dash. Much appreciated! – StefanHanotin Dec 05 '11 at 14:35
  • Even though this is an old question somebody might still come here. You probably want to use ABS() around the datediff to make the difference absolute. Or else you might run into problems with a requirement like <= 35 since events that are earlier will have a negative diff. – Tim Strijdhorst Sep 23 '14 at 12:20
1

Try the following:

SELECT * FROM `dates` WHERE DATEDIFF(`dates`.`expiry`, `dates`.`payment`) <= 35;
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114