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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Selecting dates within last 12 years

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2009-01-12 : 08:10:25
How can I select dates in a table which fall within the last 12 years please?

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-12 : 08:15:07
select * from urtable where year(empdate) <= year(getdate())-12
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-12 : 08:23:46
select * from urtable
where createdon between dateadd(yy,-12,getdate()) and getdate()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-12 : 09:05:52
If you need to consider time

where datecol >=dateadd(yy,-12,dateadd(day,datediff(day,0,getdate()),0)) and
datecol <=getdate()


Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-12 : 09:08:41
if you want till todays records

where datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) and
datecol <dateadd(dd,dateadd(dd,0,getdate()),0)


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-12 : 09:19:08
quote:
Originally posted by visakh16

if you want till todays records

where datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) and
datecol <dateadd(dd,dateadd(dd,0,getdate()),0)


This will also give you FULL last 12 year (beginning from January 1st), and not the current day 12 years ago.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-12 : 09:25:00
quote:
Originally posted by Peso

quote:
Originally posted by visakh16

if you want till todays records

where datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) and
datecol <dateadd(dd,dateadd(dd,0,getdate()),0)


This will also give you FULL last 12 year (beginning from January 1st), and not the current day 12 years ago.



E 12°55'05.63"
N 56°04'39.26"



but wasnt that the reqmnt? OP never specified its current day 12 years ago
Go to Top of Page
   

- Advertisement -