sql syntax, computed fields

basilhs_s
basilhs_s used Ask the Experts™
on
suppose we have the following select

select
a1 as t_a1,
a2 as t_a2,
from table1

is it possible somehow to say something like
select
a1 as t_a1,
a2 as t_a2,
t_a1 - t_a2 as t_a3
from table1?

i am using sql server 2000
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Yes it's possible without any problem if your fields supports such operation.
i.e. you cannot do this with char but no problems with numbers.

Commented:
better to use:

a1 as t_a1,
a2 as t_a2,
a1 - a2 as t_a3
from table1
No, SQL Server doesn't allow the use of column aliases in Select expressions

If you have a complex expression that you want to re-use without rewriting it, put it in a wrapping select statement:
Select *,  t_a1 - t_a2 as t_a3
From (
Select a1 as t_a1, a2 as t_a2,
from table1) As T

Author

Commented:
this is exactly what i was looking for

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial