---
title: Functions and CASE
summary: Value functions in GET and WHERE — CASE, COALESCE, GREATEST and LEAST, rounding and maths, text functions, and the conversions between text and numbers — with their rules for units, missing values and mismatches.
order: 80
terms: [pset]
see-also: [reference/bql/get, reference/bql/where, reference/bql/unit]
---

# Functions and CASE

Next to arithmetic (`+ - * /`, `ABS`), an expression can call value functions and choose between values with `CASE`. They work in `GET` items, on either side of a comparison in `WHERE`, inside each other and inside aggregates (`MAX(GREATEST(a, b))`). Unlike arithmetic they also produce text and booleans.

```bql {#functions-intro}
GET qid, COALESCE(property.Pset_WallCommon.FireRating, "unknown") AS Rating, GREATEST(calculated.quantity.width, attribute.OverallWidth) UNIT mm AS Width WHERE type = IFCDOOR
```

A `GET` item that is a function or a `CASE` is labelled by its text unless it has an alias, so give it one with `AS`. `GROUP BY` and `ORDER BY` take features, not expressions.

## Rules shared by all functions {#rules}

- **No value in, no value out.** A function with a missing operand has no value — except `COALESCE`, `GREATEST`, `LEAST` and `CASE`, which exist to deal with missing values.
- **A wrong kind of operand is no value, not an error.** `UPPER` of a number, or `ROUND` of a text, has no value for that object. The wrong *number* of arguments is rejected before the query runs ([[error:BimWorksQueryError.INVALID_FUNCTION_CALL]]).
- **Numbers carry their unit.** A step, a modulus or a bound must have the dimension of the value it applies to: `ROUND(quantity.*.Width, 50 mm)`. `ROUND(quantity.*.Width)` has no value — to the nearest one of what?
- **Positions are 1-based.**
- Text and boolean literals can be written as arguments and as `CASE` results: `"unknown"`, `true`.

## CASE {#case}

```text {#case-syntax}
CASE WHEN <condition> THEN <value> [WHEN <condition> THEN <value> ...] [ELSE <value>] END
```

The first branch whose condition holds supplies the value. A condition is anything that can follow `WHERE` — including paths through relations — and means exactly what it means there: a comparison on a feature the object has no value for is false, so the branch is skipped. Without `ELSE`, an object that matches no branch has no value.

```bql {#case-label}
GET qid, CASE WHEN property.*.LoadBearing = true AND property.*.IsExternal = false THEN "core structure" WHEN calculated.quantity.length < 5 m THEN "short partition" ELSE "other" END AS WallClass WHERE type = IFCWALL
```

A `CASE` can be compared like a feature, and on its own it is true when it has a value:

```bql {#case-where}
GET qid WHERE CASE WHEN type = IFCDOOR THEN attribute.OverallWidth WHEN type = IFCWINDOW THEN attribute.OverallHeight END > 2 m
```

`CASE ALL` gives every value whose condition holds, in the order of the branches, and `ELSE` only when none held. It is for the multi-valued virtual features — a classification with a code per level ([[page:reference/bfl#case-all]]); where one value is expected it is its first.

## Missing values: COALESCE, GREATEST, LEAST {#missing}

| Function | Result |
|---|---|
| `COALESCE(a, b, …)` | The first operand that has a value, of any kind. |
| `GREATEST(a, b, …)` | The largest number; operands without a value are skipped. |
| `LEAST(a, b, …)` | The smallest number; operands without a value are skipped. |

`GREATEST` and `LEAST` convert units (`GREATEST(quantity.*.Width, 900 mm)`); operands of different dimensions have no value.

```bql {#coalesce-where}
GET qid WHERE COALESCE(property.Pset_WallCommon.FireRating, property.*.Brandwerendheid) = null AND type = IFCWALL
```

## Numbers {#numbers}

| Function | Result |
|---|---|
| `ROUND(x [, step])` | `x` rounded half up to a multiple of `step`. |
| `FLOOR(x [, step])`, `CEIL(x [, step])` | Rounded down, rounded up. |
| `MOD(x, y)` | The remainder of `x / y`, never negative for a positive `y`. |
| `POWER(x, n)` | `x` to the power `n`. A value with a unit needs a whole `n`: `POWER(length, 2)` is an area. |
| `SQRT(x)` | The square root; the square root of an area is a length. |

Without a step, `ROUND`, `FLOOR` and `CEIL` round to whole numbers and accept only values without a unit.

```bql {#round-bands}
GET ROUND(quantity.*.Length, 500 mm) UNIT m AS Band, qid WHERE type = IFCBEAM
```

## Text {#text}

| Function | Result |
|---|---|
| `CONCAT(a, b, …)` | The operands joined. Takes text, booleans and numbers without a unit; print a quantity with `TO_STRING(x, mm)` first. |
| `UPPER(t)`, `LOWER(t)`, `TRIM(t)` | Upper case, lower case, without surrounding whitespace. |
| `LEFT(t, n)`, `RIGHT(t, n)` | The first, the last `n` characters. |
| `SUBSTRING(t, from [, length])` | From position `from`, to the end or for `length` characters. |
| `SPLIT_PART(t, separator, n)` | The `n`-th part; a negative `n` counts from the end. No such part: no value. |
| `REPLACE(t, search, replacement)` | Every occurrence replaced. Literal and case-sensitive. |
| `REGEX_EXTRACT(t, regex [, group])` | The first match: group 1 when the pattern has groups, otherwise the whole match. No match: no value. |
| `CHAR_LENGTH(t)` | The number of characters. |

`REGEX_EXTRACT` is case-insensitive, like `MATCHES_REGEX`; start the pattern with `(?-i)` to make it case-sensitive. A pattern that backtracks without end is abandoned for that object, which then has no value.

```bql {#split-code}
GET qid, SPLIT_PART(classification.identification, ".", 1) AS ElementGroup WHERE SPLIT_PART(classification.identification, ".", 1) IN ("21", "22")
```

Comparisons on a text result follow the rules for text features ([[bql:where#strings]]): `=` ignores case, `CS =` does not, and `!=` and `NOT IN` also select objects for which the expression has no value.

## Conversions {#conversions}

| Function | Result |
|---|---|
| `TO_NUMBER(t [, unit])` | The number written in a text (`0.24` or `0,24`). With a unit the result is a quantity: `TO_NUMBER(property.*.Dikte, mm)`. Text that is not a number: no value. |
| `TO_STRING(x [, unit] [, decimals])` | The value as text. A quantity needs the unit to print in; the unit symbol is not appended. Without `decimals`: at most six, trailing zeros dropped. |

```bql {#to-number}
GET qid WHERE TO_NUMBER(REGEX_EXTRACT(property.Pset_WallCommon.FireRating, "[0-9]+")) >= 60 AND type = IFCWALL
```

## Reserved words {#reserved}

The function names and `CASE`, `WHEN`, `THEN`, `ELSE`, `END` are reserved. A property set or property with such a name is written in quotes: `property."END"."LEFT"`.
