Groups | Blog | Home
all groups > sql server (alternate) > may 2004 >

sql server (alternate) : Using alias in an expression


Gunnar Vøyenli
5/21/2004 11:28:33 AM
Hi
I tried to use the alias a1 for the column f1 like this, but it fails:

create table a (f1 int not null primary key)
insert into a (f1) values (2)

select f1 a1 from a where a1 = 2

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'a1'.

In my real situation f1 is a "pretty complex" calculation, and a1 is an
alias of that.
I wanted to use the alias in the query instead of repeating the calculation
several times.

If this use of alias is not legal, then is there a way to get around this?

TIA
Gunnar Vøyenli
EDB-konsulent as
NORWAY

David Portas
5/21/2004 11:58:31 AM
Put your aliased column in a derived table query.

SELECT a1
FROM
(SELECT f1 AS a1
FROM A) AS A
WHERE a1 = 2

--
David Portas
SQL Server MVP
--

Gunnar Vøyenli
5/21/2004 2:27:57 PM
Thanks alot, David!
Now, its working fine!
-Gunnar
[quoted text, click to view]

AddThis Social Bookmark Button