1

I have following expression in my where clause:

DA.Access_Date >= DATEADD(YEAR, -2, GETDATE())

But it returns data till '2015-02-17' i.e. current year minus two.

I want data of two full years and current year

e.g. 2015-01-01 to till date. Any inputs on this will be appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

5 Answers5

2

Try this : Here DATEADD(yy, DATEDIFF(yy,0,getdate()) will give start month of the year

DA.Access_Date >= DATEADD(YEAR, -2, DATEADD(YY, DATEDIFF(YY,0,GETDATE()), 0))
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
0

Your condition should be like below. DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE())-2,0) this will returns first day of 2015 year.

DA.Access_Date >=  DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE())-2,0)
Tharunkumar Reddy
  • 2,773
  • 18
  • 32
0

With the help of YEAR scalar function

WHERE
YEAR(DA.Access_Date) in (YEAR(GETDATE()),YEAR(GETDATE())-1,YEAR(GETDATE())-2)
knkarthick24
  • 3,106
  • 15
  • 21
0

Just compare the year.

Try

YEAR(DA.Access_Date) >= (YEAR(GETDATE()) - 2)
Fabien TheSolution
  • 5,055
  • 1
  • 18
  • 30
0

You should try this in where condition. Year(DA.Access_Date) >= Year(getdate()) - 2