Can I have a CASE Statement in the WHERE Clause?

A very common question I get asked is whether I can have a CASE Statement in the WHERE Clause. There are a number of examples of CASE being used in SELECT columns, or in ORDER BY that we tend to forget CASE can be used wherever an expression is expected.

Where Can I use a CASE Statement?

According to Ms SQL Docs, a CASE statement can be used throughout the SELECT statement.

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

Microsoft SQL Docs, CASE (Transact-SQL)

Example Query

Suppose we want to get all people from the Persons table whose persontype is either VC or IN. To do this with CASE you could write:

SELECT FirstName, LastName, PersonType
FROM Person.Person
WHEN PersonType = 'VC' THEN 1
WHEN PersonType = 'IN' THEN 1

In this example CASE returns a one if PersonType matches. Then, since 1 = 1 is true, the row is returned.

OK, so now you can see that you can use CASE statement within a WHERE clause. Does that mean you should?

Personally I think it comes down to readability. In this case the logic is pretty simple. Here I think it makes sense to stick with the basis. Just use Boolean OR or the IN operator.

Here is the example with OR

SELECT FirstName, LastName, PersonType
FROM Person.Person
WHERE PersonType = 'VC' or PersonType = 'IN'

Here is the same example with SQL IN

SELECT FirstName, LastName, PersonType
FROM Person.Person
WHERE PersonType in ('VC','IN')

Kris Wenzel

Kris Wenzel has been working with databases over the past 28 years as a developer, analyst, and DBA. He has a BSE in Computer Engineering from the University of Michigan and a MBA from the University of Notre Dame. Kris has written hundreds of blog articles and many online courses. He loves helping others learn SQL.

  • Andy says:

    Hi Kris,

    You answered my initial question as to why would I use a CASE in your example as I’d of generally used the IN, so could you provide an example of where you would definitely prefer to use a CASE?


  • Kris Wenzel says:

    Hi, I’m more apt to use a CASE in the column list or order by. I don’t think I would use it in a WHERE, even though it is legal.


    • Ricky Urban says:


      Up to now, I had only used CASE in my select clause. I just developed a query, though, which checked the timestamp in order to deliver the results of same day versus previous day. The reason for the need is that we run a 2 shift operation and 2nd shift runs until 1AM. We wanted a query where the user would not have to change anything, and would instead just run the query as is to get their day’s results. By using CASE in the WHERE clause, we were able to accomplish this.

  • Aaron says:

    I used it for a report query when a field was returning ‘Y’ or null and I was comparing it to a boolean parameter:

    WHERE @OnPublishedProgram = CASE
    WHEN On_Published_Program = ‘Y’ THEN 1
    ELSE 0

  • peter says:

    Does using CASE statement cause table scan where IN or OR will not (granted the column in indexed). Thx.

    • Kris Wenzel says:

      Hi, The optimizer will try to use an index. In the simple case I provided it verified it did use a clustered index scan, but I imagine as your CASE gets more complicated there’s less a chance, especially one with several columns to evaluate within the expressions.

  • >