3.4.2. Scalar functions

Scalar functions return a single value.

The length() and size() functions are quite similar, and so it is important to take note of the difference. Owing to backwards compatibility, length() currently works on four types: strings, paths, lists and pattern expressions. However, it is recommended to use length() only for paths, and the size() function for strings, lists and pattern expressions. length() on those types may be deprecated in future.

The function toInt() has been superseded by toInteger(), and will be deprecated in a future release.

Functions:

Figure 3.21. Graph
alt

3.4.2.1. coalesce()

Returns the first non-null value in the list of expressions passed to it. In case all arguments are null, null will be returned.

Syntax: coalesce(expression [, expression]*)

Arguments:

Name Description

expression

The expression that may return null.

Query. 

MATCH (a)
WHERE a.name = 'Alice'
RETURN coalesce(a.hairColor, a.eyes)

Table 3.113. Result
coalesce(a.hairColor, a.eyes)

1 row

"brown"

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE a.name = 'Alice' RETURN coalesce(a.hairColor, a.eyes)

3.4.2.2. endNode()

endNode() returns the end node of a relationship.

Syntax: endNode(relationship)

Arguments:

Name Description

relationship

An expression that returns a relationship.

Query. 

MATCH (x:Developer)-[r]-()
RETURN endNode(r)

Table 3.114. Result
endNode(r)

2 rows

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

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

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (x:Developer)-[r]-() RETURN endNode(r)

3.4.2.3. head()

head() returns the first element in a list.

Syntax: head(expression)

Arguments:

Name Description

expression

This expression should return a list of some kind.

Query. 

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

The first element in the list is returned.

Table 3.115. Result
a.array head(a.array)

1 row

["one","two","three"]

"one"

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE a.name = 'Eskil' RETURN a.array, head(a.array)

3.4.2.4. id()

Returns the id of the relationship or node.

Syntax: id(expression)

Arguments:

Name Description

expression

An expression that returns a node or a relationship.

Query. 

MATCH (a)
RETURN id(a)

This returns the node id for all the nodes.

Table 3.116. Result
id(a)

5 rows

0

1

2

3

4

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) RETURN id(a)

3.4.2.5. last()

last() returns the last element in a list.

Syntax: last(expression)

Arguments:

Name Description

expression

This expression should return a list of some kind.

Query. 

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

The last element in the list is returned.

Table 3.117. Result
a.array last(a.array)

1 row

["one","two","three"]

"three"

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE a.name = 'Eskil' RETURN a.array, last(a.array)

3.4.2.6. length()

To return or filter on the length of a path, use the length() function.

Syntax: length(path)

Arguments:

Name Description

path

An expression that returns a path.

Query. 

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

The length of the path p is returned by the query.

Table 3.118. Result
length(p)

3 rows

2

2

2

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH p = (a)-->(b)-->(c) WHERE a.name = 'Alice' RETURN length(p)

3.4.2.7. properties()

properties() converts the arguments to a map of its properties. If the argument is a node or a relationship, the returned map is a map of its properties. If the argument is already a map, it is returned unchanged.

Syntax: properties(expression)

Arguments:

Name Description

expression

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

Query. 

CREATE (p:Person { name: 'Stefan', city: 'Berlin' })
RETURN properties(p)

Table 3.119. Result
properties(p)

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

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

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) CREATE (p:Person {name: 'Stefan', city: 'Berlin'}) RETURN properties(p)

3.4.2.8. size()

To return or filter on the size of a list, use the size() function.

Syntax: size(list)

Arguments:

Name Description

list

An expression that returns a list.

Query. 

RETURN size(['Alice', 'Bob'])

Table 3.120. Result
size(['Alice', 'Bob'])

1 row

2

The number of items in the list is returned by the query.

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN size(['Alice', 'Bob'])

3.4.2.9. Size of pattern expression

This is the same size() method described before, but instead of passing in a list directly, you provide a pattern expression that can be used in a match query to provide a new set of results. These results are a list of paths. The size of the result is calculated, not the length of the expression itself.

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 3.121. Result
fof

1 row

3

The number of sub-graphs matching the pattern expression is returned by the query.

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE a.name = 'Alice' RETURN size((a)-->()-->()) AS fof

3.4.2.10. Size of string

To return or filter on the size of a string, use the size() function.

Syntax: size(string)

Arguments:

Name Description

string

An expression that returns a string.

Query. 

MATCH (a)
WHERE size(a.name)> 6
RETURN size(a.name)

Table 3.122. Result
size(a.name)

1 row

7

The size of the name 'Charlie' is returned by the query.

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE size(a.name) > 6 RETURN size(a.name)

3.4.2.11. startNode()

startNode() returns the starting node of a relationship.

Syntax: startNode(relationship)

Arguments:

Name Description

relationship

An expression that returns a relationship.

Query. 

MATCH (x:Developer)-[r]-()
RETURN startNode(r)

Table 3.123. Result
startNode(r)

2 rows

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

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

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (x:Developer)-[r]-() RETURN startNode(r)

3.4.2.12. timestamp()

timestamp() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. It will return the same value during the whole one query, even if the query is a long running one.

Syntax: timestamp()

Query. 

RETURN timestamp()

The time in milliseconds is returned.

Table 3.124. Result
timestamp()

1 row

1504856085025

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN timestamp()

3.4.2.13. toBoolean()

toBoolean() converts the argument to a boolean. It attempts to convert a string to a boolean value, and if passed a boolean, it will leave it unchanged. If the parsing fails, null will be returned.

Syntax: toBoolean(expression)

Arguments:

Name Description

expression

An expression that returns a boolean or a string.

Query. 

RETURN toBoolean('TRUE'), toBoolean('not a boolean')

Table 3.125. Result
toBoolean('TRUE') toBoolean('not a boolean')

1 row

true

<null>

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN toBoolean('TRUE'), toBoolean('not a boolean')

3.4.2.14. toFloat()

toFloat() converts the argument to a float. A string is parsed as if it was an floating point number. If the parsing fails, null will be returned. An integer will be cast to a floating point number.

Syntax: toFloat(expression)

Arguments:

Name Description

expression

An expression that returns anything.

Query. 

RETURN toFloat('11.5'), toFloat('not a number')

Table 3.126. Result
toFloat('11.5') toFloat('not a number')

1 row

11.5

<null>

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN toFloat('11.5'), toFloat('not a number')

3.4.2.15. toInteger()

toInteger() converts the argument to an integer. A string is parsed as if it was an integer number. If the parsing fails, null will be returned. A floating point number will be cast into an integer.

Syntax: toInteger(expression)

Arguments:

Name Description

expression

An expression that returns anything.

Query. 

RETURN toInteger('42'), toInteger('not a number')

Table 3.127. Result
toInteger('42') toInteger('not a number')

1 row

42

<null>

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN toInteger('42'), toInteger('not a number')

3.4.2.16. type()

Returns a string representation of the relationship type.

Syntax: type(relationship)

Arguments:

Name Description

relationship

A relationship.

Query. 

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

The relationship type of r is returned by the query.

Table 3.128. Result
type(r)

2 rows

"KNOWS"

"KNOWS"

Try this query live.  CREATE (alice:Developer {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (n)-[r]->() WHERE n.name = 'Alice' RETURN type(r)