Would someone please tell me why the following SQL Statement does not work.

I have a mssql statement to select last years data.  the statement is:
Select Count(LeadID) ld From Leaddata where year(CapptDate) = DATEADD(YEAR, -1, getdate())

Open in new window


According to my data, 2018 has 1 as an answer, 2017 has 76 as an answer.  when i plug in the actual years ie '2017' in place of the dateadd, i get the proper results.  The above statement should return 76 but it returns zero.  

Any idea?
williamj67Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Try changing the criteria to this.

YEAR(CapptDate) =YEAR(DATEADD(YEAR, -1, getdate()))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
williamj67Author Commented:
That fixed it.  i don't quite understand why all the documentation on the web does not mention that it does make sense.  Thanks
Ramesh D JaiswalCommented:
You query is not working because you are comparing year(Integer value) to a datetime.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I have a mssql statement to select last years data

or something like this:

where year(CapptDate) = year(getdate())-1

Open in new window

Scott PletcherSenior DBACommented:
NOHHHH.  Don't use functions on a table column unless it's absolutely unavoidable (which it rarely is).  It makes the condition "nonsargable", which can really hurt performance because SQL can't best use indexes than (and after I, as a DBA, build all those perfect indexes for you!).


Select Count(LeadID) ld
From Leaddata
where CapptDate >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) AND
    CapptDate < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)

Btw, that's a good general pattern to use for date queries.  For example, if you wanted yesterday's data only:

where CapptDate >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 1, 0) AND
    CapptDate < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.