BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using aliases with SQL server

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Gerhard Venter
Guest





PostPosted: Fri Oct 08, 2004 5:46 pm    Post subject: Using aliases with SQL server Reply with quote



In Sybase it is possible to have a select statement as follows

select T.*, (some condition) as Alias1, (if some condition then "true" else
Alias1) as Alias2
from Table T

As you can see one can refer to and use the value of Alias1 in the second
field. Is this possible in MS SQL Server 2000. If it is how do I do it?

Thanks
Gerhard


Back to top
Alain Quesnel
Guest





PostPosted: Fri Oct 08, 2004 6:36 pm    Post subject: Re: Using aliases with SQL server Reply with quote



You can use a case statement.

From MSSQL Books Online:

USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price


--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Gerhard Venter"
Quote:
In Sybase it is possible to have a select statement as follows

select T.*, (some condition) as Alias1, (if some condition then "true"
else
Alias1) as Alias2
from Table T

As you can see one can refer to and use the value of Alias1 in the second
field. Is this possible in MS SQL Server 2000. If it is how do I do it?

Thanks
Gerhard





Back to top
Gerhard Venter
Guest





PostPosted: Fri Oct 08, 2004 7:16 pm    Post subject: Re: Using aliases with SQL server Reply with quote



Thanks for the reply but that was not quite what I meant. Here is my real
example maybe it is a bit clearer now.

select C.*,
(case
when CITY <> '' then
(case
when STATE <> '' then
(case
when ZIP <> '' then CITY + ', ' + STATE + ' ' + ZIP else
CITY + ', ' + STATE
end)
when ZIP <> '' then CITY + ', ' + STATE + ' ' + ZIP
when ZIP <> '' then CITY + ', ' + ZIP else CITY
end)
when STATE <> '' then
(case
when ZIP <> '' then CITY + ', ' + STATE + ' ' + ZIP else CITY +
', ' + STATE
end)
when ZIP <> '' then CITY + ', ' + STATE + ' ' + ZIP
when ZIP <> '' then CITY + ', ' + ZIP
when STATE <> '' then
(case
when ZIP <> '' then STATE + ' ' + ZIP else STATE
end)
when ZIP <> '' then STATE + ' ' + ZIP
when ZIP <> '' then ZIP else null

end) as 'CITY_ST_ZIP', <<<<<<< here I alias the first
field (Alias1)
ADDR1 as ADDR_LINE1,
(case
when coalesce(ADDR2,'') <> '' then ADDR2 else CITY_ST_ZIP
<<<<< error here CITY_ST_ZIP is unknown column
end) as ADDR_LINE2,
I refer here to the alias in previous field
(case
I want to use its value
when coalesce(ADDR2,'') <> '' then CITY_ST_ZIP else null
end) as ADDR_LINE3
from
DM.COMPANY C


Thanks

Gerhard.

"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> wrote

Quote:
You can use a case statement.

From MSSQL Books Online:

USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price


--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Gerhard Venter" news:4166d26f$1 (AT) newsgroups (DOT) borland.com...
In Sybase it is possible to have a select statement as follows

select T.*, (some condition) as Alias1, (if some condition then "true"
else
Alias1) as Alias2
from Table T

As you can see one can refer to and use the value of Alias1 in the
second
field. Is this possible in MS SQL Server 2000. If it is how do I do it?

Thanks
Gerhard







Back to top
Tomislav Kardaš
Guest





PostPosted: Sun Oct 10, 2004 4:06 pm    Post subject: Re: Using aliases with SQL server Reply with quote

Hi Gerhard!

On Fri, 8 Oct 2004 10:46:25 -0700, "Gerhard Venter"
<gerhard.venter (AT) comtronic (DOT) com> wrote:

Quote:
In Sybase it is possible to have a select statement as follows

select T.*, (some condition) as Alias1, (if some condition then "true" else
Alias1) as Alias2
from Table T

As you can see one can refer to and use the value of Alias1 in the second
field. Is this possible in MS SQL Server 2000. If it is how do I do it?

not possible.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.