467,187 Members | 1,321 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,187 developers. It's quick & easy.

Using alias in an expression

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 Vyenli
EDB-konsulent as
NORWAY
Jul 20 '05 #1
  • viewed: 3470
Share:
2 Replies
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
--
Jul 20 '05 #2
Thanks alot, David!
Now, its working fine!
-Gunnar
"David Portas" <RE****************************@acm.org> wrote in message
news:Fa********************@giganews.com...
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
--

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gunnar Liknes | last post: by
9 posts views Thread by Ron | last post: by
68 posts views Thread by Jim Langston | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.