472,139 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

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 Vøyenli
EDB-konsulent as
NORWAY
Jul 20 '05 #1
2 3616
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
22 posts views Thread by Daniel Rucareanu | last post: by
68 posts views Thread by Jim Langston | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.