?
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
Medium Priority
?
301 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 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 1600 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

809 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