Solved

T-SQL: Can you reference a SELECT column expression in the WHERE clause without repeating the entire expression?

Posted on 2010-11-19
5
292 Views
Last Modified: 2012-05-10
The following won't work, right?
select FName+LName as FullName
from Employees
where FullName='Montgomery Scott'
Is there any way to reference the result of the column expression (FName+LName) using relative ordering, like this?
select FName+LName
from Employees
where [Column 0]='Montgomery Scott'
Or are you stuck repeating the entire expression like this?
select FName+LName
from Employees
where FName+LName='Montgomery Scott'
Does someone have a trick or a workaround?

I know this works but I'm trying to avoid a needless nested/derived table:
select * from (select FName+LName as FullName
from Employees) MyDerivedTable
where FullName='Montgomery Scott'
0
Comment
Question by:ZuZuPetals
5 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 34175986
>Does someone have a trick or a workaround?
sorry, but you have found all the details about this "issue"
you have to repeat the expression, or use a derived table

unless you consider to create, in the table, a computed column "fullname" with that expression ...

0
 
LVL 4

Expert Comment

by:Andre412
ID: 34176287
Im guessing it would be too obvious split the where

select FName+LName as FullName
from Employees
where FullName='Montgomery Scott'

becomes

select FName+LName as FullName
from Employees
where FName= 'Montgomery' and LName = 'Scott'

your only alternative is select from the select

Select fullname From
       (select firstname + ' ' + surname [fullname]
      from [user]) U
      where U.[fullname] like '%fred%'
0
 
LVL 3

Accepted Solution

by:
DFW_Ed earned 400 total points
ID: 34176332
here's the oder of operations:

http://www.bennadel.com/blog/70-SQL-Query-Order-of-Operations.htm

because the compiler hasn't gotten to the "select" list before reading the "where" clause you're not able to use derived columns like you can in an "order by"
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34185873
No, You cannot directly use the object in where condition. you have to put full expression.
0
 
LVL 2

Author Closing Comment

by:ZuZuPetals
ID: 34188481
The order of operations really helped me--thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now