Cypher Coalesce Function

coalesce() returns the first non-null value in the given list of expressions.

Syntax: coalesce(expression [, expression]*)

Returns: The type of the value returned will be that of the first non-null expression.

Arguments

NameDescription
expressionAn expression which may return null.

Example

RETURN coalesce(null, 3, 7, null, 2)

The value returned will be the first non-null expression among all the expressions passed to the function, which in this case will be 3.

Considerations

  • coalesce(null) returns null.
  • null will be returned if all the arguments are null.

Example

RETURN coalesce(2, null, "Hello, World!", true, -1, 2020)

Returns the following Result

coalesce(2, null, "Hello, World!", true, -1, 2020)
2

Example

RETURN coalesce(null, "Hello, World!", true, -1, 2020, 2)

Returns the following Result

coalesce(null, "Hello, World!", true, -1, 2020, 2)
"Hello, World!"

Example

RETURN coalesce(null, null, true, -1, 2020, 2)

Returns the following Result

coalesce(null, null, true, -1, 2020, 2)
true

Example

RETURN coalesce("Hello, World!")

Returns the following Result

coalesce("Hello, World!")
"Hello, World!"

Example

MATCH (a)
WHERE a.name="Narendra Modi"
RETURN coalesce(a.age, a.DOB)

Returns the value of age of Narendra Modi if it is not null, or else it returns the value of DOB if it is not null, else it returns null. Considering that the age of Narendra Modi is not null, the following Result is returned.

coalesce(a.age, a.DOB)
69

Example

RETURN coalesce(null, null, null, null, null, null)

Returns the following Result

coalesce(null, null, null, null, null, null)
null

Example

RETURN coalesce(null)

Returns the following Result

coalesce(null)
null

Note-This is just a special case of the above example where all values are null.

Learn More