SQL Variables used with a SELECT Clause

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:

USE Northwind
GO
declare @age VARCHAR(4), @years VARCHAR(4)
set @age = DATEDIFF(YY,BirthDate,GETDATE())
set @years = DATEDIFF(YY,HireDate,GETDATE())

SELECT FirstName, Birthdate, Hiredate
FROM Employees
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?
mnoisetteAsked:
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
What am I doing wrong?
This is wrong:
set @age = DATEDIFF(YY,BirthDate,GETDATE())
 set @years = DATEDIFF(YY,HireDate,GETDATE())

You can solve this by rewriting the WHERE criteria:
SELECT FirstName, Birthdate, Hiredate
FROM Employees
WHERE (DATEDIFF(YY,BirthDate,GETDATE()) + DATEDIFF(YY,HireDate,GETDATE())) >85

Open in new window

mnoisetteAuthor Commented:
That's how I originally wrote it and it works fine.
I wanted to write it with a variable if that were possible.
Why can't I define the expression with a variable?
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why can't I define the expression with a variable?
Because you need to write a select to bring the data and you'll need to guarantee that the select only returns one row. Something like:
declare @age INT, @years INT -- Need to be Integer and not Varchar so you can perform the add operation
 
 SELECT @age = DATEDIFF(YY,BirthDate,GETDATE()), 
    @years = DATEDIFF(YY,HireDate,GETDATE())
 FROM Employees
 WHERE EmployeeID=1 -- Here you'll need to guarantee that it returns only one row since the variables can't handle more than one value

SELECT FirstName, Birthdate, Hiredate
 FROM Employees
 WHERE (@age + @years) >85

Open in new window

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
mnoisetteAuthor Commented:
Makes sense...
Got it!
Thanks.
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
Microsoft SQL Server

From novice to tech pro — start learning today.