Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL CASE statement with OR?

Say I have to following:

Select OrderID = 
    Case OrderID 
        When 1 Then 'Customer1'
        When 2 Then 'Customer2'
        When 3 Then 'Customer2'
        Else 'Unknown Customer'
    End 
From OrdersPlaced

Is it possible to add an or and do something along the lines of:

Select OrderID = 
    Case OrderID 
        When 1 Then 'Customer1'
        When 2 Or 3 Then 'Customer2'
        Else 'Unknown Customer'
    End 
From OrdersPlaced
like image 362
user2125348 Avatar asked May 20 '14 19:05

user2125348


People also ask

What is a simple CASE statement in SQL?

In this format, we evaluate one expression against multiple values. In a simple case statement, it evaluates conditions one by one. Once the condition and expression are matched, it returns the expression mentioned in THEN clause. We have following syntax for a case statement in SQL with a simple expression ...

What is the syntax of the case expression in SQL?

The syntax of the SQL CASE expression is: CASE [expression] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... WHEN condition_n THEN result_n ELSE result END case_name The CASE statement can be written in a few ways, so let’s take a look at these parameters. The parameters or components of the CASE SQL statement are:

What is case_name in SQL?

case_name (optional): This value indicates what the column should be referred to as when displayed on the screen or from within a subquery. It’s also called the column alias. There are actually two ways to use an SQL CASE statement, which are referred to as a “simple case expression” or a “searched case expression”.

What is the difference between case and else in SQL?

The CASE statement returns the result_1, result_2, or result_3 if the expression matches the corresponding expression in the WHEN clause. If the expression does not match any expression in the WHEN clause, it returns the esle_result in the ELSE clause. The ELSE clause is optional.


3 Answers

There are two forms of CASE expression, 'searched' and 'simple'. You can't use an OR with a 'simple' CASE expression, but you can with the 'searched' form:

Case  
    When OrderID = 1 Then 'Customer1'
    When OrderID = 2 Or
         OrderID = 3 Then 'Customer2'
    Else 'Unknown Customer'
End 

Or even

Case  
    When OrderID = 1 Then 'Customer1'
    When OrderID IN (2, 3) Then 'Customer2'
    Else 'Unknown Customer'
End 
like image 95
p.s.w.g Avatar answered Sep 20 '22 16:09

p.s.w.g


You can use alternative form of CASE

Select OrderID = 
    Case 
        When OrderID = 1 Then 'Customer1'
        When OrderID = 2 Or OrderID = 3 Then 'Customer2'
        Else 'Unknown Customer'
    End 
From OrdersPlaced
like image 24
Yuriy Galanter Avatar answered Sep 18 '22 16:09

Yuriy Galanter


CASE expression has two kinds of syntax - the simple (i.e. one that you are showing), and the searched, with multiple logical conditions. If you would like to use an OR, you need the second kind:

Select OrderID = 
    Case 
        When OrderID =1 Then 'Customer1'
        When OrderID =2 Or OrderID =3 Then 'Customer2'
        Else 'Unknown Customer'
    End 
From OrdersPlaced

Here is the description from the documentation:

  • The simple CASE expression compares an expression to a set of simple expressions to determine the result.
  • The searched CASE expression evaluates a set of Boolean expressions to determine the result.
like image 34
Sergey Kalinichenko Avatar answered Sep 19 '22 16:09

Sergey Kalinichenko