Conditional Selection of Column

  • 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.

  • 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

  • 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]

  • Thank you very much that was exactly what I needed to fix it up. You rock

  • 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