Search code examples
sqlsql-servercase-expression

Using Keyword [IN] with Case Expression in a SQL Statement


I have an query like this , but it does not work , what's wrong with IN keyword with CASE EXPRESSION ?

Select State = case c.addressId
  when in('2552','2478','2526') then 'IN' 
  when in ('9999') then 'OUT'
  else 'UNKNOWN'
  end,
  name, time
from x;

I've use SQL Server 2008 and this is the error msg :

Incorrect syntax near the keyword 'in'.


Solution

  • You've got the syntax wrong. It should be CASE WHEN [COLUMN] in (...):

    Select 
      case when c.addressId in('2552','2478','2526') then 'IN' 
      when c.addressId in ('9999') then 'OUT'
      else 'UNKNOWN'
      end as State,
      name, time
    from contact c;