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
 General SQL Server Forums
 New to SQL Server Programming
 Setting values for Aliased Columns

Author  Topic 

buckeyeJane
Starting Member

11 Posts

Posted - 2011-06-16 : 12:38:20
Hello -
I have a select statement that is pulling information that I need. However, I need to add aliased columns and set the value on the fly during the select statement. For instance, the customer id is < 0, I would need the aliased column to state "Hidden" for that particular row. How can this be accomplished in the select statement?

Thank you in advance for any help or advice you can provide!

BuckeyJane!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 12:42:44
Show us a data example.

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

Subscribe to my blog
Go to Top of Page

buckeyeJane
Starting Member

11 Posts

Posted - 2011-06-16 : 12:47:34
An example of the data that I would like to have is as follows:

CustomerID Device Status
-1 HP printer Hidden

Select customername, device, ???????? as status
where customername = 1 or customerid = -1
Order by Device
Go to Top of Page

buckeyeJane
Starting Member

11 Posts

Posted - 2011-06-16 : 13:00:56
I actually figured this out on my own.. :) I think.... I used a case statement... If there is a better way to do it, please let me know. :)

CASE
WHEN dbo.device.customerid < 0
THEN 'Hidden'
ELSE
CASE
WHEN dbo.device.customerid > 0 and
dbo.device.deviceid < 0
THEN 'Unmanaged'
ELSE
CASE
WHEN dbo.device.customerid > 0 and
dbo.device.deviceid > 0
THEN 'Managed'
END
END
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 13:39:10
Ah I see. So you were confusing us when you were saying a column alias. You are actually just adding your "alias" as data and not an actual column alias.

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

Subscribe to my blog
Go to Top of Page

buckeyeJane
Starting Member

11 Posts

Posted - 2011-06-16 : 14:08:58
Hi Tara - Yes. I wasn't real sure how to word that. Sorry for the confusion. I do appreciate your help and assistance... :)
Go to Top of Page
   

- Advertisement -