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 2000 Forums
 Transact-SQL (2000)
 Using a column alias in a case statement

Author  Topic 

mtavares
Starting Member

1 Post

Posted - 2003-02-03 : 10:36:52
I was wondering if there is some way to use a column alias in a case statement like the following:

select 1 as 'TestColumn',
Case TestColumn
WHEN 1 THEN 'This is True'
ELSE 'This is False'
END AS CaseTest
FROM TestTable

Right now this would give me an error saying that TestColumn is an invalid column name. Is there any way to make this work using the aliased name? I know this looks pointless, but this query is being built on the fly generically where testcolumn and the casetest columns would be strings in a table that are pulled out and concatenated to make a select query. Any help would be greatly appreciated.

Thanks,
Mike

X002548
Not Just a Number

15586 Posts

Posted - 2003-02-03 : 10:42:41
I would try:

SELECT TestColumn, CaseTest From (
select 1 as TestColumn,
Case TestColumn
WHEN 1 THEN 'This is True'
ELSE 'This is False'
END AS CaseTest
FROM TestTable ) As XXX

Good Luck

Brett

8-)

Go to Top of Page
   

- Advertisement -