11

I know you cannot use a alias column in the where clause for T-SQL; however, has Microsoft provided some kind of workaround for this?

Related Questions:

2 Answers 2

28

One workaround would be to use a derived table.

For example:

select *
from 
   (
   select a + b as aliased_column
   from table
   ) dt
where dt.aliased_column = something.

I hope this helps.

1
  • I have always just repeated the SQL, this is soooo much better!
    – Paul C
    Oct 23, 2012 at 12:20
1

Depending on what you are aliasing, you could turn it into a user defined function and reference that in both places. Otherwise your copying the aliased code in several places, which tends to become very ugly and means updating 3+ spots if you are also ordering on that column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.