Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an Alias column in the where clause in ms-sql 2000

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:

  • Unknown Column In Where Clause
  • Can you use an alias in the WHERE clause in mysql?
  • “Invalid column name” error on SQL statement from OpenQuery results
like image 746
wusher Avatar asked Nov 04 '08 00:11

wusher


2 Answers

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.

like image 182
Jim V. Avatar answered Nov 03 '22 01:11

Jim V.


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.

like image 32
Adam Avatar answered Nov 03 '22 01:11

Adam