Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 alias as function argument or in where clause

Author  Topic 

sarahmfr
Posting Yak Master

214 Posts

Posted - 2010-04-21 : 14:52:46
Is there a way to use alias names for columns as arguments for a function.
example
select assign, score, point, fn_lettergrade(score/point),
sum(score) as totscore, sum(point) as totpoint, fn_lettergrade(totscore/totpoint)

.....

It gave me an error
I got the same error on trying to include an alias in a where clause

sarah

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-21 : 15:29:18
You can if you use a derived table.

SELECT Column1, dbo.Function(NewName)
FROM (SELECT Column1, Column2 AS NewName) dt

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sarahmfr
Posting Yak Master

214 Posts

Posted - 2010-04-21 : 21:35:43
Thanks I will try it

sarah
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-21 : 23:59:57
quote:
Originally posted by tkizer

You can if you use a derived table.

SELECT Column1, dbo.Function(NewName)
FROM (SELECT Column1, Column2 AS NewName FROM Table)td
Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 00:39:43
Yes, FROM TABLE!

SELECT Column1, dbo.Function(NewName)
FROM (SELECT Column1, Column2 AS NewName FROM SomeTable) dt

sarahmfr, think of a derived table as an inline view.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-22 : 02:30:24
or use the expression itself

select assign, score, point, fn_lettergrade(score/point),
sum(score) as totscore, sum(point) as totpoint, fn_lettergrade(sum(score) /sum(point))

.....


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Asken
Starting Member

38 Posts

Posted - 2010-04-22 : 03:17:00
quote:
Originally posted by madhivanan

or use the expression itself

select assign, score, point, fn_lettergrade(score/point),
sum(score) as totscore, sum(point) as totpoint, fn_lettergrade(sum(score) /sum(point))




Yeah, I'd go for that as well...


Reporting & Analysis Specialist
Helping others helps me get better...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 11:30:45
Yes of course you can use the expression, but I thought the whole point of this topic was so that you wouldn't have to repeat the expression.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -