Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conditional Selection of Column Expand / Collapse
Author
Message
Posted Monday, April 13, 2009 12:11 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 10, 2013 6:46 AM
Points: 86, Visits: 145
I could really use some help on this one. I am trying to select the Company column if it has a value or fname + lastname if it does not. Here is what I am thinking, I know this doesn't work.
SELECT [ID]
,[Title]
,[fname]
,[lastname]
,[Company]
,[Attention]
,if(Company != null or Company != '')
[Company] as displayName
else
fname + ' ' + lastname as displayName
FROM [Donors]

Any help, guidence or other constructive comments would be greatly appreciated.
Post #696019
Posted Monday, April 13, 2009 12:14 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 9:58 AM
Points: 13,872, Visits: 9,600
You're on the right track, but what you need is "Case When Else". Look up Case in Books Online, it's got samples, syntax, etc.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #696022
Posted Monday, April 13, 2009 12:21 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, November 8, 2013 2:06 PM
Points: 138, Visits: 70
Hey,

find the following soln...

SELECT [ID]
,[Title]
,[fname]
,[lastname]
,[Company]
,[Attention]

, case when (Company is not null or len(Company) <> 0)
then [Company]
else fname + ' ' + lastname
end as displayName

FROM [Donors]


Post #696032
Posted Monday, April 13, 2009 12:22 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 10, 2013 6:46 AM
Points: 86, Visits: 145
Thank you very much that was exactly what I needed to fix it up. You rock
Post #696034
Posted Monday, April 13, 2009 12:43 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, October 2, 2013 9:39 AM
Points: 329, Visits: 195
sreddy (4/13/2009)
Hey,

find the following soln...

SELECT [ID]
,[Title]
,[fname]
,[lastname]
,[Company]
,[Attention]

, case when (Company is not null or len(Company) <> 0)
then [Company]
else fname + ' ' + lastname
end as displayName

FROM [Donors]




Try this too.

SELECT [ID]
,[Title]
,[fname]
,[lastname]
,[Company]
,[Attention]
, IsNull( NullIf(Company, ''),fname+' '+ lastname) as displayname

FROM [Donors]


Regards
Vinay
Post #696053
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse