How do I pull records for the current year using the system date?

How do I pull records for the current year (2011) using the system date?  And for the prior and current year (2010 & 2011) using the system date.
LoriLHSAsked:
Who is Participating?
 
Ephraim WangoyaCommented:

select * from yourtable
where DATEPART(YEAR, yourdatefield) = DATEPART(YEAR, GETDATE())  --this year
or DATEPART(YEAR, yourdatefield) = DATEPART(YEAR, DATEADD(YY, -1, GETDATE()))  --last year
0
 
sarabhaiCommented:
Did you need records between 1 April to 31 March.
0
 
LoriLHSAuthor Commented:
All of current year up til today.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
sarabhaiCommented:
It means, suppose todays u need records from 1 jan 2011 to 5 april 2011.
0
 
Ephraim WangoyaCommented:

Use GETDATE()


select * from yourtable
where DATEPART(YEAR, yourdatefield) = DATEPART(YEAR, GETDATE())


0
 
LoriLHSAuthor Commented:
Thank you.
0
 
sarabhaiCommented:
try this one...
select * From T_table
where d_column between convert( datetime , convert(char(4),year(getdate())) +'-'+ convert(char(4), '1')+'-' + convert(char(4),'1') )
      and convert( datetime , convert(char(4),year( getdate())) +'-'+ convert(char(4),month( getdate()))+'-' + convert(char(4),day( getdate())) )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.