if statements in SQL view

I have an sql view with 3 tables which I have described the basic layout of below. I want to create a view to show the main table with the address. If the column [External] has Yes in it, then I want to pull the address from the external table, if the value is no then I want to pull it from the internal table. Is this easy enough to do? Any examples would be appreciated.

TableMain
ID
Name
External
LinkID

TableInternal
LinkID
Address1
Address2
Address3

TableExternal
LinkID
Address1
Address2
Address3
LVL 6
CaptainGibletsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Jim HornMicrosoft SQL Server Data DudeCommented:
Give this a whirl..
Replace 'Yes' and 'No' with 1 and 0 if it's a bit field, and '???' with whatever floats your boat
SELECT tm.ID, tm.Name, 
   CASE cm.External
      WHEN 'Yes' THEN te.address1
      WHEN 'No' THEN ti.address1
      ELSE '???' END as address1, 
   CASE cm.External
      WHEN 'Yes' THEN te.address2
      WHEN 'No' THEN ti.address2
      ELSE '???' END as address2, 
   CASE cm.External
      WHEN 'Yes' THEN te.address3
      WHEN 'No' THEN ti.address3
      ELSE '???' END as address3
FROM TableMain tm
   LEFT JOIN TableInternal ti ON tm.LinkID = ti.LinkID
   LEFT JOIN TableExternal te ON tm.LinkID = te.LinkID

Open in new window

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this:
SELECT M.ID, M.Name, M.External, E.Address1, E.Address2, E.Address3
FROM TableMain M
	INNER JOIN TableExternal E ON M.LinkID = E.LinkID
WHERE External='YES'
UNION ALL
SELECT M.ID, M.Name, M.External, I.Address1, I.Address2, I.Address3
FROM TableMain M
	INNER JOIN TableInternal I ON M.LinkID = I.LinkID
WHERE External='NO'

Open in new window

Scott PletcherSenior DBACommented:
SELECT m.ID, m.name,
    COALESCE(i.Address1, e.Address1) AS Address1,
    COALESCE(i.Address2, e.Address2) AS Address2,
    COALESCE(i.Address3, e.Address3) AS Address3
FROM TableMain m
LEFT OUTER JOIN TableInternal i ON m.[External] = 'No'  AND i.LinkID = m.ID
LEFT OUTER JOIN TableExternal e ON m.[External] = 'Yes' AND e.LinkID = m.ID
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

From novice to tech pro — start learning today.