Scalar functions

Scalar functions return a single value.

Functions:

The length() and size() functions are quite similar, and so it is important to take note of the difference.

Function length()

Only works for paths.

Function size()

Only works for the three types: strings, lists, and pattern expressions.

Diagram

1. coalesce()

The 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:

Name Description

expression

An expression that may return null.

Considerations:

null will be returned if all the arguments are null.

Query
MATCH (a)
WHERE a.name = 'Alice'
RETURN coalesce(a.hairColor, a.eyes)
Table 1. Result
coalesce(a.hairColor, a.eyes)

"brown"

Rows: 1

2. endNode()

The function endNode() returns the end node of a relationship.

Syntax: endNode(relationship)

Returns:

A Node.

Arguments:

Name Description

relationship

An expression that returns a relationship.

Considerations:

endNode(null) returns null.

Query
MATCH (x:Developer)-[r]-()
RETURN endNode(r)
Table 2. Result
endNode(r)

Node[2]{name:"Charlie",age:53,eyes:"green"}

Node[1]{name:"Bob",age:25,eyes:"blue"}

Rows: 2

3. head()

The function head() returns the first element in a list.

Syntax: head(expression)

Returns:

The type of the value returned will be that of the first element of the list.

Arguments:

Name Description

expression

An expression that returns a list.

Considerations:

head(null) returns null.

head([]) returns null.

If the first element in list is null, head(list) will return null.

Query
MATCH (a)
WHERE a.name = 'Eskil'
RETURN a.liked_colors, head(a.liked_colors)

The first element in the list is returned.

Table 3. Result
a.liked_colors head(a.liked_colors)

["pink","yellow","black"]

"pink"

Rows: 1

4. id()

The function id() returns a node or a relationship identifier, unique by an object type and a database. Therefore, it is perfectly allowable for id() to return the same value for both nodes and relationships in the same database. For examples on how to get a node and a relationship by ID, see Get node or relationship by id.

Neo4j implements the id so that:

Node

Every node in a database has an identifier. The identifier for a node is guaranteed to be unique among other nodes' identifiers in the same database, within the scope of a single transaction.

Relationship

Every relationship in a database has an identifier. The identifier for a relationship is guaranteed to be unique among other relationships' identifiers in the same database, within the scope of a single transaction.

Syntax: id(expression)

Returns:

An Integer.

Arguments:

Name Description

expression

An expression that returns a node or a relationship.

Considerations:

id(null) returns null.

Query
MATCH (a)
RETURN id(a)

The node identifier for each of the nodes is returned.

Table 4. Result
id(a)

0

1

2

3

4

Rows: 5

5. last()

The function last() returns the last element in a list.

Syntax: last(expression)

Returns:

The type of the value returned will be that of the last element of the list.

Arguments:

Name Description

expression

An expression that returns a list.

Considerations:

last(null) returns null.

last([]) returns null.

If the last element in list is null, last(list) will return null.

Query
MATCH (a)
WHERE a.name = 'Eskil'
RETURN a.liked_colors, last(a.liked_colors)

The last element in the list is returned.

Table 5. Result
a.liked_colors last(a.liked_colors)

["pink","yellow","black"]

"black"

Rows: 1

6. length()

The function length() returns the length of a path.

Syntax: length(path)

Returns:

An Integer.

Arguments:

Name Description

path

An expression that returns a path.

Considerations:

length(null) returns null.

Query
MATCH p = (a)-->(b)-->(c)
WHERE a.name = 'Alice'
RETURN length(p)

The length of the path p is returned.

Table 6. Result
length(p)

2

2

2

Rows: 3

7. properties()

The function properties() returns a map containing all the properties; the function can be utilized for a relationship or a node. If the argument is already a map, it is returned unchanged.

Syntax: properties(expression)

Returns:

A Map.

Arguments:

Name Description

expression

An expression that returns a relationship, a node, or a map.

Considerations:

properties(null) returns null.

Query
CREATE (p:Person {name: 'Stefan', city: 'Berlin'})
RETURN properties(p)
Table 7. Result
properties(p)

{city -> "Berlin", name -> "Stefan"}

Rows: 1
Nodes created: 1
Properties set: 2
Labels added: 1

8. randomUUID()

The function randomUUID() returns a randomly-generated Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID). This is a 128-bit value with strong guarantees of uniqueness.

Syntax: randomUUID()

Returns:

A String.

Query
RETURN randomUUID() AS uuid
Table 8. Result
uuid

"23e45576-eb65-4940-8328-b3979c99ec10"

Rows: 1

A randomly-generated UUID is returned.

9. size()

The function size() returns the number of elements in a list.

Syntax: size(list)

Returns:

An Integer.

Arguments:

Name Description

list

An expression that returns a list.

Considerations:

size(null) returns null.

Query
RETURN size(['Alice', 'Bob'])
Table 9. Result
size(['Alice', 'Bob'])

2

Rows: 1

The number of elements in the list is returned.

10. size() applied to pattern expression

This is the same function size() as described above, but you pass in a pattern expression, instead of a list. The function size will then calculate on a list of paths.

Syntax: size(pattern expression)

Arguments:

Name Description

pattern expression

A pattern expression that returns a list.

Query
MATCH (a)
WHERE a.name = 'Alice'
RETURN size((a)-->()-->()) AS fof
Table 10. Result
fof

3

Rows: 1

The number of paths matching the pattern expression is returned. (The size of the list of paths).

11. size() applied to string

The function size() returns the number of Unicode characters in a string.

Syntax: size(string)

Returns:

An Integer.

Arguments:

Name Description

string

An expression that returns a string value.

Considerations:

size(null) returns null.

Query
MATCH (a)
WHERE size(a.name) > 6
RETURN size(a.name)
Table 11. Result
size(a.name)

7

Rows: 1

The number of characters in the string 'Charlie' is returned.

12. startNode()

The function startNode() returns the start node of a relationship.

Syntax: startNode(relationship)

Returns:

A Node.

Arguments:

Name Description

relationship

An expression that returns a relationship.

Considerations:

startNode(null) returns null.

Query
MATCH (x:Developer)-[r]-()
RETURN startNode(r)
Table 12. Result
startNode(r)

Node[0]{name:"Alice",age:38,eyes:"brown"}

Node[0]{name:"Alice",age:38,eyes:"brown"}

Rows: 2

13. timestamp()

The function timestamp() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

It is the equivalent of datetime().epochMillis.

Syntax: timestamp()

Returns:

An Integer.

Considerations:

timestamp() will return the same value during one entire query, even for long-running queries.

Query
RETURN timestamp()

The time in milliseconds is returned.

Table 13. Result
timestamp()

1633007907729

Rows: 1

14. toBoolean()

The function toBoolean() converts a string, integer or boolean value to a boolean value.

Syntax: toBoolean(expression)

Returns:

A Boolean.

Arguments:

Name Description

expression

An expression that returns a boolean, string or integer value.

Considerations:

toBoolean(null) returns null.

If expression is a boolean value, it will be returned unchanged.

If the parsing fails, null will be returned.

If expression is the integer value 0, false will be returned. For any other integer value true will be returned.

This function will return an error if provided with an expression that is not a string, integer or boolean value.

Query
RETURN toBoolean('true'), toBoolean('not a boolean'), toBoolean(0)
Table 14. Result
toBoolean('true') toBoolean('not a boolean') toBoolean(0)

true

<null>

false

Rows: 1

15. toBooleanOrNull()

The function toBooleanOrNull() converts a string, integer or boolean value to a boolean value. For any other input value, null will be returned.

Syntax: toBooleanOrNull(expression)

Returns:

A Boolean or null.

Arguments:

Name Description

expression

Any expression that returns a value.

Considerations:

toBooleanOrNull(null) returns null.

If expression is a boolean value, it will be returned unchanged.

If the parsing fails, null will be returned.

If expression is the integer value 0, false will be returned. For any other integer value true will be returned.

If the expression is not a string, integer or boolean value, null will be returned.

Query
RETURN toBooleanOrNull('true'), toBooleanOrNull('not a boolean'), toBooleanOrNull(0), toBooleanOrNull(1.5)
Table 15. Result
toBooleanOrNull('true') toBooleanOrNull('not a boolean') toBooleanOrNull(0) toBooleanOrNull(1.5)

true

<null>

false

<null>

Rows: 1

16. toFloat()

The function toFloat() converts an integer, floating point or a string value to a floating point number.

Syntax: toFloat(expression)

Returns:

A Float.

Arguments:

Name Description

expression

An expression that returns a numeric or a string value.

Considerations:

toFloat(null) returns null.

If expression is a floating point number, it will be returned unchanged.

If the parsing fails, null will be returned.

This function will return an error if provided with an expression that is not an integer, floating point or a string value.

Query
RETURN toFloat('11.5'), toFloat('not a number')
Table 16. Result
toFloat('11.5') toFloat('not a number')

11.5

<null>

Rows: 1

17. toFloatOrNull()

The function toFloatOrNull() converts an integer, floating point or a string value to a floating point number. For any other input value, null will be returned.

Syntax: toFloatOrNull(expression)

Returns:

A Float or null.

Arguments:

Name Description

expression

Any expression that returns a value.

Considerations:

toFloatOrNull(null) returns null.

If expression is a floating point number, it will be returned unchanged.

If the parsing fails, null will be returned.

If the expression is not an integer, floating point or a string value, null will be returned.

Query
RETURN toFloatOrNull('11.5'), toFloatOrNull('not a number'), toFloatOrNull(true)
Table 17. Result
toFloatOrNull('11.5') toFloatOrNull('not a number') toFloatOrNull(true)

11.5

<null>

<null>

Rows: 1

18. toInteger()

The function toInteger() converts a boolean, integer, floating point or a string value to an integer value.

Syntax: toInteger(expression)

Returns:

An Integer.

Arguments:

Name Description

expression

An expression that returns a boolean, numeric or a string value.

Considerations:

toInteger(null) returns null.

If expression is an integer value, it will be returned unchanged.

If the parsing fails, null will be returned.

If expression is the boolean value false, 0 will be returned. If expression is the boolean value true, 1 will be returned.

This function will return an error if provided with an expression that is not a boolean, floating point, integer or a string value.

Query
RETURN toInteger('42'), toInteger('not a number'), toInteger(true)
Table 18. Result
toInteger('42') toInteger('not a number') toInteger(true)

42

<null>

1

Rows: 1

19. toIntegerOrNull()

The function toIntegerOrNull() converts a boolean, integer, floating point or a string value to an integer value. For any other input value, null will be returned.

Syntax: toIntegerOrNull(expression)

Returns:

An Integer or null.

Arguments:

Name Description

expression

Any expression that returns a value.

Considerations:

toIntegerOrNull(null) returns null.

If expression is an integer value, it will be returned unchanged.

If the parsing fails, null will be returned.

If expression is the boolean value false, 0 will be returned. If expression is the boolean value true, 1 will be returned.

If the expression is not a boolean, floating point, integer or a string value, null will be returned.

Query
RETURN toIntegerOrNull('42'), toIntegerOrNull('not a number'), toIntegerOrNull(true), toIntegerOrNull(['A', 'B', 'C'])
Table 19. Result
toIntegerOrNull('42') toIntegerOrNull('not a number') toIntegerOrNull(true) toIntegerOrNull(['A', 'B', 'C'])

42

<null>

1

<null>

Rows: 1

20. type()

The function type() returns the string representation of the relationship type.

Syntax: type(relationship)

Returns:

A String.

Arguments:

Name Description

relationship

An expression that returns a relationship.

Considerations:

type(null) returns null.

Query
MATCH (n)-[r]->()
WHERE n.name = 'Alice'
RETURN type(r)

The relationship type of r is returned.

Table 20. Result
type(r)

"KNOWS"

"KNOWS"

Rows: 2