coalesce Functie

Van toepassing op:gemarkeerd met ja Databricks SQL-controle gemarkeerd als ja Databricks Runtime

Retourneert het eerste niet-null-argument.

Syntaxis

coalesce(expr1 [, ...] )

Argumenten

Retourneert

Het resultaattype is het minst voorkomende type argumenten.

Er moet ten minste één argument zijn. In tegenstelling tot normale functies waarbij alle argumenten worden geëvalueerd voordat de functie wordt aangeroepen, coalesce worden argumenten van links naar rechts geëvalueerd totdat een niet-null-waarde wordt gevonden. Als alle argumenten zijn NULL, is NULLhet resultaat .

Voorbeelden

> SELECT coalesce(NULL, 1, NULL);
 1

-- The following example raises a runtime error because the second argument is evaluated.
>  SELECT coalesce(NULL, 5 / 0);
 Error: DIVISION_BY_ZERO

-- The following example raises no runtime error because the second argument is not evaluated.
> SELECT coalesce(2, 5 / 0);
 2

> SELECT coalesce(NULL, 'hello');
 hello