Link to home
Start Free TrialLog in
Avatar of MariaHalt
MariaHaltFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There must be a better way, right?
There is:  Build a cross-reference table.
SELECT
CASE FirstName
WHEN (select '1' from TABLE_NAME where name IN ('MARY', 'SUE', 'ELLEN')) THEN 'MOTHER'
ELSE 'OTHER'
END
FROM CastOfDallas
Avatar of MariaHalt

ASKER

Perfect...Thanks!