cdburns123 cdburns123 - 1 year ago 90
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?

Answer Source

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%'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download