Avatar of MariaHalt
MariaHalt
Flag for United States of America asked on

sql case statement

Is there a way to test for a string range in the case statement?  See my lame examples that don't work:

SELECT
CASE FirstName
WHEN IN ('MARY', 'SUE', 'ELLEN') THEN 'MOTHER'
ELSE 'OTHER'
END
FROM CastOfDallas

SELECT
CASE FirstName
WHEN ('MARY' OR 'SUE' OR 'ELLEN') THEN 'MOTHER'
ELSE 'OTHER'
END
FROM CastOfDallas

I don't want to do this, becaue the then's are the same.  There must be a better way, right?

SELECT
CASE FirstName
WHEN 'MARY'  THEN 'MOTHER'
WHEN 'SUE'   THEN 'MOTHER'
WHEN 'ELLEN' THEN 'MOTHER'
ELSE 'OTHER'
END
FROM CastOfDallas
Microsoft DevelopmentMicrosoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
MariaHalt

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Anthony Perkins

There must be a better way, right?
There is:  Build a cross-reference table.
gaurav2325

SELECT
CASE FirstName
WHEN (select '1' from TABLE_NAME where name IN ('MARY', 'SUE', 'ELLEN')) THEN 'MOTHER'
ELSE 'OTHER'
END
FROM CastOfDallas
MariaHalt

ASKER
Perfect...Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck