Reputation: 1
I have a database with fault records. I am trying to create a query to return incidents reported more than 30 days ago.
This is the code I an using:
SELECT `IncidentNumber`,
`DateReported`,
`ReportedBy`,
`AffectedSystem`,
`EquipmentName`,
`SerialNumberOfAffectedPart`,
`Notes`,
`JobStatus`
FROM `incident_tbl`
WHERE `JobStatus` != 'Closed'
AND `DateReported` < 'DATEADD(day, -30, GETDATE())'
LIMIT 0 , 100
I have read numerous posts here, and elsewhere on the internet and have no idea why this won't work.
The code simply returns all open incidents.
Any help is much appreciated.
Thanks
Upvotes: 0
Views: 625
Reputation: 44844
SELECT
`IncidentNumber` ,
`DateReported` ,
`ReportedBy` ,
`AffectedSystem` ,
`EquipmentName`,
`SerialNumberOfAffectedPart` ,
`Notes` ,
`JobStatus`
FROM `incident_tbl`
WHERE
`JobStatus` != 'Closed'
AND `DateReported` < DATE_SUB( NOW(), INTERVAL 30 day)
LIMIT 0 , 100
Upvotes: 1
Reputation: 13344
Get it outside of the quotes!! MySQL interprets that as actual string, and the function is never called to output the correct date range.
Use DATE_SUB for subtraction!
SELECT `IncidentNumber` , `DateReported` , `ReportedBy` , `AffectedSystem` , `EquipmentName`, `SerialNumberOfAffectedPart` , `Notes` , `JobStatus`
FROM `incident_tbl`
WHERE `JobStatus` != 'Closed'
AND `DateReported` < DATE_SUB( NOW(), INTERVAL 30 DAY )
LIMIT 0 , 100
Upvotes: 0