Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Anniversary Date

Author  Topic 

kattlees
Starting Member

3 Posts

Posted - 2013-10-31 : 12:51:34
I am struggling with this. We want to pull everyone that is going to have a 5 year anniversary in 2014. We have a field of DateHired which is yyyy-mm-dd format. How do I pull everyone who was hired in 2009

We would like this to automatically update each year without having to go in and change the code.

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2013-10-31 : 13:04:10
Maybe this:

select *
from yourtable
where datehired like cast(year(getdate())-5 as char(4))+'%' /* this line if datehired is char type */
where year(datehired)=year(getdate())-5 /* this line if datehired is date/datetime type */
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-11-01 : 07:44:38
WHERE
datehired>=dateadd(year,datediff(year,0,getdate())-4,0) and
datehired<dateadd(year,datediff(year,0,getdate())-3,0)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -