> For the complete documentation index, see [llms.txt](https://upsolver.gitbook.io/content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://upsolver.gitbook.io/content/reference-1/functions-and-operators/operators/conditional/case.md).

# CASE

The standard SQL `CASE` expression has two forms:

[CASE ... WHEN](#case-...-when)&#x20;

The simple form searches each `value` expression from left to right until it finds one that equals the given `expression` and then returns the matching `result`.

[CASE WHEN ...](#case-when-...)

The searched form evaluates each Boolean `condition` from left to right until one is true and returns the matching `result`.

## `CASE ... WHEN`

### Syntax

```sql
CASE <expression>
    WHEN <value> THEN <result>
    [ WHEN ... ]
    [ ELSE <result> ]
END
```

### Returns

The `result` for the first `value` found that matches the `expression` is returned.&#x20;

If no match is found, the `result` from the `ELSE` clause is returned if it exists; otherwise `null` is returned.&#x20;

### Examples

#### SQL

```sql
CREATE JOB function_operator_example
    ADD_MISSING_COLUMNS = true	
AS INSERT INTO default_glue_catalog.upsolver_samples.orders_transformed_data 
  MAP_COLUMNS_BY_NAME
    SELECT 
        ordertype,
        (CASE LOWER(ordertype)
            WHEN 'shipping' THEN true
            WHEN 'pickup' THEN false
            ELSE false
        END) AS is_shipped
    FROM default_glue_catalog.upsolver_samples.orders_raw_data 
    WHERE $commit_time BETWEEN run_start_time() AND run_end_time()
    LIMIT 3;
```

#### Query result

| ordertype | is\_shipped |
| --------- | ----------- |
| SHIPPING  | true        |
| shipping  | true        |
| pickup    | false       |

## `CASE WHEN ...`

### Syntax

```sql
CASE
    WHEN <condition> THEN <result>
    [ WHEN ... ]
    [ ELSE <result> ]
END
```

### Returns

The `result` corresponding to the first true `condition` is returned.

If no conditions are true, the `result` from the `ELSE` clause is returned if it exists; otherwise `null` is returned.

### Examples

#### SQL

```sql
CREATE JOB function_operator_example
    ADD_MISSING_COLUMNS = true	
AS INSERT INTO default_glue_catalog.upsolver_samples.orders_transformed_data 
        MAP_COLUMNS_BY_NAME
    SELECT 
        customer_email,
        (CASE
            WHEN customer_email LIKE '%@gmail.com' THEN 'gmail'
            WHEN customer_email LIKE '%@yahoo.com' THEN 'yahoo'
        END) AS email_domain
    FROM default_glue_catalog.upsolver_samples.orders_raw_data 
    WHERE $commit_time BETWEEN run_start_time() AND run_end_time()
    LIMIT 3;
```

#### Query result

| customer\_email                    | email\_domain |
| ---------------------------------- | ------------- |
| <Melissa.Whitej@comcast.net>       | `NULL`        |
| <Patricia.Johnsonfp@hotmail.co.uk> | `NULL`        |
| <Amanda.Thompsonam@gmail.com>      | gmail         |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://upsolver.gitbook.io/content/reference-1/functions-and-operators/operators/conditional/case.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
