April 13, 2009 at 12:11 pm
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.
April 13, 2009 at 12:14 pm
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
April 13, 2009 at 12:21 pm
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]
April 13, 2009 at 12:22 pm
Thank you very much that was exactly what I needed to fix it up. You rock
April 13, 2009 at 12:43 pm
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
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply