Felix
Felix

Reputation: 13

sql statement with datediff and colums from db

I have a problem with DATEDIFF in this sql-statement:

$sqlab = "select * from _database12";
$sqlab .= " where DATEDIFF (d,EntryDate,StartDate) < 14";
$sqlab .= " and EntryDate >= '".$_POST["from"]."%'";
$sqlab .= " and EntryDate <= '".$_POST["till"]."%'";
$sqlab .= " order by EntryDate DESC";

Without DATEDIFF this works perfectly, but i need to get it to work with DATEDIFF in order to only get those entries, where StartDate minus EntryDate is less than 14 days.

EntryDate ans StartDate are colums in database12. Can I use these?

Thanks in advance

Upvotes: 1

Views: 97

Answers (1)

jacouh
jacouh

Reputation: 8741

You are using erroneous format of DATEDIFF - a Transact-SQL DATEDIFF(datepart , startdate , enddate)? reference.

In MySQL, here is reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff, DATEDIFF(enddate, startdate) given in days, has no format/datepart argument.

You can try this if StartDate >= EntryDate:

$sqlab = "SELECT * FROM _database12
 WHERE DATEDIFF(StartDate, EntryDate) < 14
 AND EntryDate >= '" . $_POST["from"] . "'
 AND EntryDate <= '" . $_POST["till"] . "'
 ORDER BY EntryDate DESC";

Upvotes: 2

Related Questions