Link to home
Start Free TrialLog in
Avatar of McGurk1
McGurk1Flag for United States of America

asked on

TSQL get records for last two years

I need create a query to get the records for the last two years.

SELECT     LastActivityDate
FROM         MyTable
WHERE       LastActivityDate >= dateadd(yy, datediff(yy,-0, getdate()),-2)   

Open in new window


This did not give me the number of records that I know need to be part of the query.

I appreciate any help.
Avatar of Aneesh
Aneesh
Flag of Canada image

the best option will be to catch @@rowcount value after running the query, or else you can add a count(*) query as part your existing one

SELECT     LastActivityDate, cnt = (SELECT  count(*)
FROM         MyTable
WHERE       LastActivityDate >= dateadd(yy, datediff(yy,-0, getdate()),-2)  )
FROM         MyTable
WHERE       LastActivityDate >= dateadd(yy, datediff(yy,-0, getdate()),-2)
ASKER CERTIFIED SOLUTION
Avatar of David Todd
David Todd
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of McGurk1

ASKER

Thanks.  This gives me what I needed.