I have a SELECT that I want to use a variable with for an expression, but the variable(s) contains columns from my database.
Here is my SQL statement:
declare @age VARCHAR(4), @years VARCHAR(4)
set @age = DATEDIFF(YY,BirthDate,GETDATE())
set @years = DATEDIFF(YY,HireDate,GETDATE())
SELECT FirstName, Birthdate, Hiredate
WHERE (@age + @years) >85
I receive the following errors:
Msg 207, Level 16, State 1, Line 4
Invalid column name 'BirthDate'.
Msg 207, Level 16, State 1, Line 5
Invalid column name 'HireDate'.
What am I doing wrong?