'Using php query with datediff
I have a php application using a MSSQL database, and am having problem with the following code:
$rstmp3 = CustomQuery("select count(ToolkitItemPk) as total FROM dbo.Toolkit_ComplianceItems WHERE LastCompleted IS NOT NULL AND (DATEDIFF(day, WarningDate, getdate()) < 0) AND (DATEDIFF(day, NextDue, getdate()) AND DepotComplianceFk='".$datatmp2['DepotComplianceFk']."'");
$datatmp3 = db_fetch_array($rstmp3);
if($datatmp3["total"]) $totalOK = $datatmp3["total"]; else $totalOK = 0;
I suspect the
(DATEDIFF(day, WarningDate, getdate()) 0) < AND (DATEDIFF(day, NextDue, getdate())
Is not the correct way to do this, as I am getting the following error:
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
I admit to being out of my depth here, so if someone could please recommend another way of doing this then that will earn my eternal gratitude.
I did have an easier statement in my table which was:
CASE
WHEN LastCompleted IS NULL THEN 'Missing'
WHEN LastCompleted IS NOT NULL AND (DATEDIFF(day, NextDue, getdate()) > 0) THEN 'Overdue'
WHEN LastCompleted IS NOT NULL AND (DATEDIFF(day, WarningDate, getdate()) > 0) AND (DATEDIFF(day, NextDue, getdate()) < 0) THEN 'Pending'
WHEN LastCompleted IS NOT NULL AND (DATEDIFF(day, WarningDate, getdate()) < 0) AND (DATEDIFF(day, NextDue, getdate()) < 0) THEN 'OK'
ELSE 'N/A'
END AS Status,
This would have made life simpler to query, but as it isnt a physical field within the database, the column returns as unknown.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
