0

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

2 Answers 2

1
 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
Sign up to request clarification or add additional context in comments.

1 Comment

This is a duplication of my earlier answer, with no explanation. Why did it get the upvote and not my answer?
0

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

1 Comment

Perfect! Thanks very much. Apologies for the simplicity, I'm somewhat of a novice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.