SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Can you use an alias in the WHERE clause?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rock_query
Yak Posting Veteran

52 Posts

Posted - 04/07/2013 :  16:44:32  Show Profile  Reply with Quote
Here is the code I have:

SELECT FirstName + ' ' + MiddleName + ' ' + LastName AS
concatName
FROM Person.Person
WHERE NOT concatName IS NULL
ORDER BY LastName

Here is the error message I am receiving:

Msg 207, Level 16, State 1, Line 17
Invalid column name 'concatName'.

I am using SQL Express 2012 on my PC. From this, it appears as though an alias cannot be used in the WHERE clause. Is this true, or is there a way to work around this?

robvolk
Most Valuable Yak

USA
15658 Posts

Posted - 04/07/2013 :  16:55:54  Show Profile  Visit robvolk's Homepage  Reply with Quote
The only way to refer to the alias is to rewrite the statement as a subquery or a common table expression. It's probably easier to write it like this:
SELECT FirstName + ' ' + MiddleName + ' ' + LastName AS
concatName
FROM Person.Person
WHERE FirstName + MiddleName + LastName IS NOT NULL
ORDER BY LastName
Since the empty strings '' are constants you don't need to include them in the WHERE condition to check for NULLs.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000