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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case When Between statement help

Status
Not open for further replies.

Zonie32

Technical User
Jan 13, 2004
242
US
Hi all,

Can someone tell me what I am doing wrong with this Case statement? If I comment out the "between" lines the query works fine. when i put them back in I get the error message 'Incorrect syntax near between'.

CASE procedure_code
when between '27301' and '27599' then 'KNEE INJ'
when between '29866' and '29889' then 'KNEE INJ'
when between '29850' and '29851' then 'KNEE INJ'
when between '73721' and '73723' then 'KNEE INJ'
when between '73560' and '73565' then 'KNEE INJ'
when '20610' then 'KNEE INJ'
when '29530' then 'KNEE INJ'
ELSE 'Nothing'
end as category
 
Use this instead:

Code:
  CASE 
    when procedure_code between '27301' and '27599' then 'KNEE INJ'
    when procedure_code between '29866' and '29889' then 'KNEE INJ'
    when procedure_code between '29850' and '29851' then 'KNEE INJ'
    when procedure_code between '73721' and '73723' then 'KNEE INJ'
    when procedure_code between '73560' and '73565' then 'KNEE INJ'
    when procedure_code = '20610' then 'KNEE INJ'
    when procedure_code = '29530' then 'KNEE INJ'
  ELSE 'Nothing'
end as category
 
Totally works! Thanks so much RiverGuy.
 
That's great. I never actually noticed that the value being set was the same in each condition. However, I do believe that when the first WHEN is evaluated to true, no further checking will be done in the CASE statement. I'm not sure if SQL Server will short circuit all of the OR clauses in your code. What do you think?
 
i do not have proof, but i believe that when one condition in a series of ORed conditions is true, no further checking is done

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top