Skip to main content

Coalesce

Builds a coalesce(...) expression, usable anywhere an SQL element is accepted (typically as a Select column).

Constructor

Coalesce(...expressions: any[]): Coalesce
new Coalesce(...expressions: any[]): Coalesce

Dual-callable. Each argument is converted through the active SerializeContext (so strings, literals, Field, Raw, Param, and sub-Select are all accepted).

import { Coalesce, Select } from '@sqb/builder';

Select(Coalesce('nick_name', 'given_name', "'Unnamed'")).from('customers');
// select coalesce(nick_name, given_name, 'Unnamed') from customers

An empty argument list serializes to an empty string (Coalesce() contributes nothing to its parent expression).

Properties

KeyTypeReadonlyDescription
_typeSerializationType.COALESCE_STATEMENTYesDiscriminates this node during serialization.
_expressionsany[]NoThe arguments passed to the constructor, in order.
_aliasstring | undefinedNoSet by .as().

Methods

as()

as(alias: string): this

Sets an alias, rendered as a trailing alias after the closing paren (no AS keyword).

Coalesce('nick_name', 'given_name').as('name');
// coalesce(nick_name, given_name) name

See also