view raw
cdburns123 cdburns123 - 8 months ago 40
SQL Question

Workaround for Select AS WHERE

Is there any way in TSQL to use variable/Alias in


Select First_Name as FN from PERSON
Where FN like 'D%'

The reason I ask is what if I have nested selects. Do I have to put the entire nested select statement again in my Where clause to filter?


This is too long for a comment.

As many similar questions point out, the answer is "no". The solution is typically a CTE or subquery. This "limitation" is a property of the SQL language, not of a particular database.

SQL Server does have a third method (which is why I am answering). This is more appropriate for a complex expression. Your example isn't a great example, but you can use outer apply:

select p2.fn
from person p outer apply
     (select p.First_Name as FN) p2
where p2.fn like 'D%'