SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Anniversary Date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kattlees
Starting Member

3 Posts

Posted - 10/31/2013 :  12:51:34  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

322 Posts

Posted - 10/31/2013 :  13:04:10  Show Profile  Reply with Quote
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

India
22744 Posts

Posted - 11/01/2013 :  07:44:38  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000