Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using CASE and IN together

Status
Not open for further replies.

jwestmore

Programmer
May 27, 2003
14
US
Hi everyone,

I'm writing a short proc and am trying to make my code easier to read by using CASE and IN together:

CASE test
WHEN IN('a','b','c') THEN '123'
WHEN IN('d','e','f') THEN '456'
WHEN IN('g','h','i') THEN '789'
END

Anyone see anything wrong syntactically here? This code generates an "Incorrect syntax near the keyword 'IN'" error message.

Thanks!
Jon
 
Code:
CASE 
WHEN test IN('a','b','c') THEN '123'
WHEN test IN('d','e','f') THEN '456'
WHEN test IN('g','h','i') THEN '789'
END
 
Jon,

Your format is correct only when using exact match.

CASE test
WHEN 'a' THEN '123'
WHEN 'b' THEN '123'
WHEN 'c' THEN '123'
WHEN 'd' THEN '456'
<etc>

When doing any other comparison, the CASE statement needs to be like swampBoogie's suggestion.

-SQLBill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top