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 pull data based on date range

I am pulling data that contains two columns with dates. I only want to return rows of data where date one, last_update occurred within 7 days of date two, program_end_day.

I have tried several variations of the following but this returns rows where last_update occurred years before the program_end_day

and last_updated >= DATEADD(DAY, 7, program_end_day)

What I expect to be returned are rows in which the last_updated date is equal to program_end_day or within 7 days of program_end_day. Something like

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

program_end_day last_updated
2024-3-13 2024-3-15
2024-3-20 2024-3-26

I have found answers to similar questions but instead of pulling the date from a column its a date the user inputs manually or using the today variable

>Solution :

SELECT * FROM PulledData
WHERE  last_updated BETWEEN
 program_end_day 
 AND 
 DATE_ADD(program_end_day, INTERVAL 7 DAY)

MySQL fiddle

SQL Server fiddle

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