Nested Case SQL

I am getting an error on this portion of my sql statement.

Trying do the following:

If Feet > 0 then I want to evaluate ItemID.
   If ItemID starts with 'LUM-TUBE' then Units will be 'FT' else they wil be Units
 If ItemID starts with 'LUM-CHANNEL' then Units will be 'LBS' else they wil be Units

Thanks!
CASE WHEN Feet > 0 
                THEN CASE  WHEN  ItemID LIKE 'LUMB-TUBE%' THEN 'FT' ELSE Units END 
         ELSE 
                    CASE WHEN  ItemID LIKE 'LUMB-CHANNEL%' THEN 'LBS' ELSE Units END 
          ELSE Units END AS Units2,

Open in new window

LVL 1
vwallaAsked:
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.

Patrick MatthewsCommented:
CASE WHEN Feet > 0 THEN
    CASE WHEN ItemID LIKE 'LUMB-TUBE%' THEN 'FT'
        WHEN ItemID LIKE 'LUMB-CHANNEL%' THEN 'LBS'
        ELSE Units END
    ELSE Units END AS Units2
0
HainKurtSr. System AnalystCommented:
with one else

case
when feet > 0 and ItemID like 'LUM-TUBE%' then 'FT'
when feet > 0 and ItemID like 'LUM-CHANNEL%' then 'LBS'
else Units
end as Units2
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
HainKurtSr. System AnalystCommented:
I mean with one case :)
0
Kevin CrossChief Technology OfficerCommented:
It looks like Patrick answered you already, but FYI your original CASE was close. You just have TWO ELSEs.

CASE
   WHEN Feet > 0
        THEN CASE  WHEN  ItemID LIKE 'LUMB-TUBE%' THEN 'FT' ELSE Units END
   ELSE CASE WHEN  ItemID LIKE 'LUMB-CHANNEL%' THEN 'LBS' ELSE Units END
   ELSE Units
END AS Units2

That should be just one:

CASE
   WHEN Feet > 0
      THEN CASE WHEN ItemID LIKE 'LUMB-TUBE%' THEN 'FT' ELSE Units END
   ELSE CASE WHEN  ItemID LIKE 'LUMB-CHANNEL%' THEN 'LBS' ELSE Units END
END AS Units2

However, I think you can probably do this like this anyway:

CASE
   WHEN Feet > 0 AND ItemID LIKE 'LUMB-TUBE%' THEN 'FT'
   WHEN Feet <= 0 AND ItemID LIKE 'LUMB-CHANNEL%' THEN 'LBS'
   ELSE Units
END AS Units2

That is basically what your first statement was doing, so if Feet <= 0 is not what you wanted to evaluate along side ItemID LIKE 'LUMB-CHANNEL%' -- see Patrick's correction which corrected the case to include both conditions nested under Feet > 0.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.