Complicated Select Case - When Statement

This is a case where I have two fields: CompanyName and AgencyName;
I display the results in a GridView but in some cases, because we have three different user groups,
the CompanyName may be blank or the AgencyName may be blank.
I try to take care of this logic on the back end but I would like to display one column on the Grid:
Company/Agency.
i thought I could do this with a SELECT...CASE.....WHEN statement but I get an error when I .
-------------------------------------
select Companyname, AgencyName
        CASE
              WHEN CompanyName <> ' ' THEN AgencyName    
              ELSE
              WHEN AgencyName <> ' ' THEN CompanyName    
            END
from client

--------------------------------------
P.S.
If I do get this working, will it be a problem when I try to sort by the column header?
Or, could I just give agencyname/companyname an alias?
jtrapat1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
here we go:
select Companyname, AgencyName 
        CASE
              WHEN CompanyName <> ' ' THEN AgencyName     
              WHEN AgencyName <> ' ' THEN CompanyName     
            END
from client 

Open in new window

0
Jinesh KamdarCommented:
>> If I do get this working, will it be a problem when I try to sort by the column header?
Or, could I just give agencyname/companyname an alias?

No, it won;t be a problem - Just give an alias to this column and include that alias in the ORDER BY clause.
0
Jinesh KamdarCommented:
Note that even if multiple conditions are true in the CASE, the action for the first condition that evaluates to true will be executed i.e. if CompanyName <> ' ' and AgencyName <> ' ', then AgencyName will be returned since it was evaluated first. If both are false, then the value specified in the ELSE part will be returned (NULL in ur case).
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

jtrapat1Author Commented:
I tried this but I get an error:
Incorrect syntax near the keyword 'CASE'.

Maybe Im missing a comma somewhere.

John
0
Jinesh KamdarCommented:
Ohh SQL Server, I guess CASE can't be used like that. You will have to use an IIF construct.

SELECT Companyname, AgencyName,
       IIF(CompanyName <> ' ', AgencyName, IIF(AgencyName <> ' ', CompanyName, 'Both are empty')) AS Temp
FROM client
ORDER BY Temp

Open in new window

0
Jinesh KamdarCommented:
I guess u missed a comma (,) after the AgencyName field in Angel's suggestion.
select Companyname, AgencyName, ---> added this comma
        CASE
              WHEN CompanyName <> ' ' THEN AgencyName     
              WHEN AgencyName <> ' ' THEN CompanyName     
        END
from client 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mfsamuelCommented:
Case can be used you are setting the Alias wrong.

...AS AgencyName
OR
"AgencyName"=Case...
select Companyname, (CASE
              WHEN CompanyName <> '' THEN AgencyName     
              WHEN AgencyName <> '' THEN CompanyName     
            END) AS AgencyName
from client

Open in new window

0
mfsamuelCommented:
sorry thought you were setting an alias to get the field in the AgencyName column.  jinesh_kamdar is correct all you are missing is a comma.
select Companyname, AgencyName, (CASE
              WHEN CompanyName <> '' THEN AgencyName     
              WHEN AgencyName <> '' THEN CompanyName     
            END) AS MergedName
from client

Open in new window

0
Jinesh KamdarCommented:
@jtrapat1: Don't u think Angel deserved the credit (read points) since he was the one to correct ur CASE syntax in the first place ???
0
mfsamuelCommented:
it was 2 things.  a comma problem and the ELSE.  I think you had both.

angel forgot the comma and also added a space which did not work.  I fixed both, and did not add the space.
0
Jinesh KamdarCommented:
@mfsamuel:
>> angel forgot the comma

The asker himself had fogotten the comma in his question. It is not an error that Angel introduced by himself.

>> I fixed both, and did not add the space

The space was specified in the question itself and so it SHOULD have been included in the solution as well - so technically speaking, ur final solution was also incorrect.

My concern here is not wat all problems u solved, but wat prompted u to solve those problems and in that regard, Angel was the first to point in the right direction.
0
mfsamuelCommented:
>>The asker himself had fogotten the comma in his question. It is not an error that Angel introduced by himself.

But that was 1/3 the reason for the query not working, so if it was not fixed the answer was not correct.

>> The space was specified in the question itself and so it SHOULD have been included in the solution as well - so technically speaking, ur final solution was also incorrect.

QUOTE: the CompanyName may be blank or the AgencyName may be blank.
That means he did not intend to have the space.

>>My concern here is not wat all problems u solved, but wat prompted u to solve those problems and in that regard, Angel was the first to point in the right direction.

There were 3 problems as I see in and my solution had all 3 corrected.  You and angel should both have assisted solution.  If the space was supposed to be there then you should get credit (for fixing the comma), angel should ge the assit, and I should get nothing.
0
Jinesh KamdarCommented:
V - I think the Asker had requested for a split. Why was then only my comment accepted as the solution? I think a split would only be fair to everyone involved.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.