SELECT logcount, logUserID, maxlogtm
, DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
FROM statslogsummary
WHERE daysdiff > 120
I get
“invalid column name daysdiff”.
Maxlogtm is a datetime field. It’s the little stuff that drives me crazy.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Normally you can’t refer to field aliases in the
WHEREclause. (Think of it as the entireSELECTincluding aliases, is applied after theWHEREclause.)But, as mentioned in other answers, you can force SQL to treat
SELECTto be handled before theWHEREclause. This is usually done with parenthesis to force logical order of operation or with a Common Table Expression (CTE):Parenthesis/Subselect:
Or see Adam’s answer for a CTE version of the same.