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.
| 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 |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-12 : 08:23:46
|
| select * from urtablewhere createdon between dateadd(yy,-12,getdate()) and getdate() |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-12 : 09:05:52
|
| If you need to consider timewhere datecol >=dateadd(yy,-12,dateadd(day,datediff(day,0,getdate()),0)) anddatecol <=getdate()MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 09:08:41
|
if you want till todays recordswhere datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) anddatecol <dateadd(dd,dateadd(dd,0,getdate()),0) |
 |
|
|
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 recordswhere datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) anddatecol <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" |
 |
|
|
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 recordswhere datecol >=dateadd(yy,datediff(yy,0,getdate())-12,0)) anddatecol <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 |
 |
|
|
|
|
|
|
|