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 statement to filter by last 5 years

I’m currently working on a SQL statement that filters the last 5 years of data I’ve added the below where clause which would take me to exactly 5 years from today.

I wanted to ask how I would improve this statement to contain all data in the 5th year as well.

e.g. The data would not be from the date 16/11/2016 but from 01/01/2016 if it was run today.

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

Where statement:

WHERE CreatedDate > DATEADD(YEAR,-5,GETDATE())

>Solution :

DATEFROMPARTS is your friend:

WHERE CreatedDate >= DATEFROMPARTS(YEAR(GETDATE())-5,1,1)
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