---
schema_version: "1"
id: 76f9d35ba864c043de844ab7dee58fb308c0d5266e9fd69c952b8d97a4641152
title: Searched case Operator
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_saql/guides/reference/bi-saql-operators-casesearch.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-operators-casesearch.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 1c3e1e4da384a180600084ef6af707058aa9b4374c66efaf9a6bf21d449a4a63
products:
  - Analytics SAQL Developer Guide
  - analytics
---

# Searched case Operator

Use `case` in a `foreach` statement to assign different field values in different situations. `case` supports two syntax forms: searched case and simple case. This section shows searched case.

## Syntax

```nolang

case
  when search_condition then result_expr
  [when search_condition2 then result_expr2 ]
  [else default_expr ]
end
```

_case...end_ opens and closes the case operator.

_when...then_ defines a conditional statement. A case expression can contain one or more conditional statements.

_search_condition_ can be any scalar expression that returns a boolean value. It can be a complex boolean expression or a nested case, as long as the result is boolean. For a list of supported operators, see [Comparison Operators](https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-operators-comparison.md).

_result_expr_ is any expression that takes a single input value and returns a single output value. Can contain values, identifiers, and scalar functions (including date and math functions). The expression must return the same data type as specified in the search condition.

_else default_expr_ (optional) is any expression that takes a single input value and returns a single output value. Can contain values, identifiers, and scalar functions (including date and math functions). The expression can return a number, string, or date.

## Usage

Statements are evaluated in the order that they are given. If the condition is `primary_expr == test_expr`, then the corresponding _result_expr_ is returned. You can specify any number of `when`/`then` statements.

You can use `else` to specify a default expression. For example, if no industry is specified, you can use the string "No Industry Specified". If you don't specify a default statement, then `null` is returned.

You can use `case` expressions in `foreach` statements. You cannot use `case` in `order`, `group`, or `filter` statements.

## Example

Suppose that you want to see the median deal size for each of your reps. You want to bin their median deal size into the buckets "Small", "Medium", and "Large". Use `case` to assign values to the median deal size.

```nolang
q = load "data";
q = group q by 'Account_Owner';
q = foreach q generate 'Account_Owner' as 'Account_Owner', median('Amount') as 'Median Amount', (case

  when median('Amount') < 1000000 then "Small"
  when median('Amount') > 1600000 then "Large"
  else "Medium"

end ) as 'Category';
```

The resulting data shows the median deal size for each rep, along with the appropriate bin label.

![Derived dimension shows time to win](https://developer.salesforce.com/media/bi_dev_guide_saql/bi_saql_caseMedAmount.png)

If you want to group the data by the field assigned by the case statement, add a `group by` statement to the end of the query. For example, to group the data by the `Category` field, add the following line to the end of the above example:

```nolang
q = group q by 'Category';
```

## Handling Null Values

In general, `null` values can’t be compared. When the search condition evaluates to `null`, the _default_expr_ is returned. If no default expression is specified, `null` is returned.
