What is the syntax or best way to have some "IF" logic within a SELECT Query

Say I already have a table which has the following fields...

MyTable1
ColPK, ColClosed, ColExpired      --both ColClosed and ColExpired are bit dataTypes (i.e. True or False)
354          1               0
355          0               0
356          0               1

I now have a second table that I want to populate from MyTable1, but my second col (MyTable2.ColStatus) is a result of ColClosed and ColExpired...

MyTable2
ColPK,    ColStatus
23           Closed
24           Open
25           Expired

How do I write a query to achieve the above results?

SELECT ColPK As 'PK', (an If statement? or can I call a Procedure?) As 'Status'
FROM dbo.MyTable

A Procedure could look like:

CREATE  Procedure dbo.GetSTATUS
      @Closed bit,
      @Expired bit,
      @STATUS nvarchar(255) Output
As
If @Closed = 1
  Begin
    Set @STATUS = 'CLOSED'
  End
Else
  If @Expired = 1
    Begin
      Set @STATUS = 'EXPIRED'
    End
  Else
    If @Closed = 0 And @Expired = 0
      Begin  
        Set @STATUS = 'OPEN'
      End
IvanHowarthAsked:
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.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT ColPK As 'PK',  CASE WHEN ColClosed = 1 THEN 'Closed'
                                               WHEN  ColExpired = 1 THEN 'Expired'  
                                               WHEN  ColClosed = 0 AND Expired =0  THEN 'Open'  As 'Status'
FROM dbo.MyTable

0
IvanHowarthAuthor Commented:
I'm getting an error message:

Incorrect syntax near the keyword 'As'.
0
Aneesh RetnakaranDatabase AdministratorCommented:
ooops missed an 'END'


SELECT ColPK As 'PK',  CASE WHEN ColClosed = 1 THEN 'Closed'
                                               WHEN  ColExpired = 1 THEN 'Expired'  
                                               WHEN  ColClosed = 0 AND Expired =0  THEN 'Open'  END  As 'Status'
FROM dbo.MyTable

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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

JimFiveCommented:
No points here, but what happens when ColClosed = 1 AND ColExpired = 1?
--
JimFive
0
IvanHowarthAuthor Commented:
Just checked out the CASE statement in Help - END was missing. Should read...

 SELECT ColPK As 'PK',  CASE WHEN ColClosed = 1 THEN 'Closed'
                                               WHEN  ColExpired = 1 THEN 'Expired'  
                                               WHEN  ColClosed = 0 AND Expired =0  THEN 'Open' END As 'Status'
FROM dbo.MyTable

Now works ok! Thanks.
0
IvanHowarthAuthor Commented:
JimFive: lol, the situation hasn't and wouldn't arise. Nevertheless, I'll include the possibility so it will return Expired :-)
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
Query Syntax

From novice to tech pro — start learning today.