using a case statement column in sql server as a parameter

I have a view in sql server 2008 where I have a column called EmployeeStatus that stores either Y or N in that column
I'm creating a stored procedure where i'm using a case state like shown below where I'm creating a new column called EmployeeStatus2

In my stored procedure I have a parameter called EmployeeStatus2 in the where clause. But when I do the code shown below, SSMS gives me a message saying  EmployeeStatus2 is not a valid column.

Any idea how I can use a column i created in a case statement as a parameter column?

SELECT [Region]
      ,[EmployeeStatus]
      ,CASE WHEN [EmployeeStatus] = 'Y' 
                      THEN 'Employed' 
                      ELSE 'Unemployed' 
                 END AS EmployeeStatus2
FROM [TestDatabase].[dbo].[TestView]
WHERE EmployeeStatus2 = @EmployeeStatus2

Open in new window

LVL 1
maqskywalkerAsked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl.  If you do a Google search for 'SQL query processing order' (or just go here), the SQL query engine processes the WHERE clause before the SELECT clause, so the WHERE clause doesn't know what EmployeeStatus2 is and throws the error.  So you have to repeat the expression in the WHERE clause.
SELECT 
   [Region],
   [EmployeeStatus],
   CASE WHEN [EmployeeStatus] = 'Y' THEN 'Employed' ELSE 'Unemployed' END AS EmployeeStatus2
FROM [TestDatabase].[dbo].[TestView]
WHERE @EmployeeStatus2 = CASE WHEN [EmployeeStatus] = 'Y' THEN 'Employed' ELSE 'Unemployed' END 

Open in new window

0

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
Anoo S PillaiCommented:
Another option -

SELECT * 
FROM ( SELECT [Region]
		,[EmployeeStatus]
		,CASE WHEN [EmployeeStatus] = 'Y' 
		THEN 'Employed' 
		ELSE 'Unemployed' 
		END AS EmployeeStatus2
	FROM [TestDatabase].[dbo].[TestView]
) AS TempData 
WHERE EmployeeStatus2 = @EmployeeStatus2

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.