Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL – Filter records by date when record contains specific ID's

I am trying to filter the RECORDS by last modified date (LastModifiedDate) column but I want this filter to work only when any record have status id’s as 6 or 7. for other ID’s it should return those records without checking for date filter.

Can anyone help me with the proper condition in whereclause of below sql?

SELECT fd.[DocID], fd.[FolderName], fd.[AStatus], fd.[LastModifiedDate]
FROM FoldersDetail fd
INNER JOIN StatusMaster sm 
    ON sm.StatusID = fd.StatusID
INNER JOIN PriorityMaster pm 
    ON pm.PriorityID = fd.PriorityID
WHERE FD.StatusID IN (6,7) AND fd.LastModifiedDate > DATEADD(day, -30, GETDATE())

This SQL filtering records but not returning other rows whose ID’s are not 6 or 7.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

SELECT fd.[DocID], fd.[FolderName], fd.[AStatus], fd.[LastModifiedDate]
FROM FoldersDetail fd
INNER JOIN StatusMaster sm 
    ON sm.StatusID = fd.StatusID
INNER JOIN PriorityMaster pm 
    ON pm.PriorityID = fd.PriorityID
WHERE FD.StatusID NOT IN (6,7) OR fd.LastModifiedDate > DATEADD(day, -30, GETDATE())

Basically if StatusID isn’t 6 or 7, the OR is satisfied, if StatusID is 6 or 7, the right part of the OR is checked.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading