case()
The case() function evaluates condition-value pairs in order and returns the value corresponding to the first condition that evaluates to true. If no condition is true, it optionally returns a default value.
Syntax
Parameters
COND1,COND2, ...- Condition expressions evaluated in order. What counts as a true condition differs by series. In the 5.0 series (through 5.0.2605.2), boolean
trueor any non-null value is treated as true — onlynulland booleanfalseare treated as false. In the 4.0 series starting with 4.0.2609.0,null, booleanfalse, the numbers0andNaN, an empty string, and an empty list, array, or map are all treated as false, and every other value is treated as true; you must also specify at least one condition-value (COND,VAL) pair, or a parse error occurs (see Error codes). VAL1,VAL2, ...- Value expressions to return when the corresponding condition is treated as true. Each value is paired with its corresponding condition.
DEFAULT- (Optional) The default value to return when all conditions are treated as false. If the total number of arguments is odd, the last argument is used as the default value.
Description
The case() function evaluates conditions in the order COND1, COND2, ..., determining whether each is true or false. When a condition is treated as true, the function immediately returns its corresponding value without evaluating further conditions.
If no condition is true, the function returns the DEFAULT value if specified, or null if no default is provided.
If a condition is treated as false, evaluation continues to the next condition.
The return type is determined by the type of the value expression that was actually evaluated. If the value expressions have different types, the result is automatically promoted to a common supertype (for example, a mix of integer and float values returns a float).
Error codes
| Error code | Description |
|---|---|
| 99000 | No condition-value pair is specified in the call (for example, calling case() alone, or with only a default value). Available starting with 4.0.2609.0. |
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Classify HTTP status codes into labels by range
table limit=5 WEB_APACHE_SAMPLE | eval label = case(status >= 500, "Server Error", status >= 400, "Client Error", status >= 300, "Redirect", "OK") | fields status, label | # label is determined based on the status value. -
Classify by HTTP method
table limit=5 WEB_APACHE_SAMPLE | eval type = case(method == "GET", "Read", method == "POST", "Submit", method == "OPTIONS", "Preflight", "Other") | fields method, type -
No condition matches and no default is provided
json "{'val': 0}" | eval result = case(val == 1, "one", val == 2, "two") | # result: null -
NULL input — null is treated as false during condition evaluation, so the default value is returned
json "{'val': null}" | eval result = case(val == 1, "one", "default") | # result: default -
Using the number 0 as a condition — result differs by series
json "{'val': 0}" | eval result = case(val, "true", "false") | # 5.0 series (through 5.0.2605.2) result: true / 4.0 series (4.0.2609.0 and later) result: falseWhether the number
0is treated as a true or false condition differs by series. In the 5.0 series (through 5.0.2605.2), any non-null value, including0, is treated as true, so this example returns"true". In the 4.0 series starting with 4.0.2609.0, the number0is treated as false, so the default value ("false") is returned instead. -
Calling without any condition-value pair — parse error in 4.0.2609.0 and later)
json "{}" | eval result = case()In the 4.0 series starting with 4.0.2609.0, because no condition-value pair is specified, a parse error (error code 99000) occurs before the query runs. The 5.0 series (through 5.0.2605.2) does not validate the argument count at parse time, so parsing succeeds; the result then depends on the execution path — the vectorized execution path fails with an error (
NullPointerException), while the row-based execution path returnsnullwithout an error.
Change history
| Target | Introduced in |
|---|---|
The case() function | Before Sonar 4.0 |
Unified truthiness criteria — the number 0, empty strings, and so on are treated as false | 4.0.2609.0 |
| Parse error (99000) when no condition-value pair is specified | 4.0.2609.0 |