Cypher Substring Function

substring() returns a substring of the original string, beginning with a 0-based index start and length.

Syntax: substring(original, start [, length])

Returns: A String.

Arguments

NameDescription
originalAn expression that returns a string.
startAn expression that returns a positive integer, denoting the position at which the substring will begin.
lengthAn expression that returns a positive integer, denoting how many characters of original will be returned.

Example

RETURN substring("Hello, World!", 2, 5)

The value returned will be string which will be sub-string of the original string. The substring will start from index 2 of the original string(inclusive of the character at index 2) and will contain the next 5 characters of the original string. In this case the substring will be “llo W”, i.e , starting from 2nd character of the original string and containing the next 5 characters of the original string.

The 3rd argument(length) is optional, which if not provided, will return a substring from the start index to the end of the original string.

Considerations

  • start uses a zero-based index, i.e, the first character in the original string has 0 index.
  • length is an optional parameter. If length is omitted, the function returns the substring starting at the position given by start and extending to the end of original.
  • If original is nullnull is returned.
  • If start is null or a negative integer, an error is raised.
  • If length is null or a negative integer, an error is raised.
  • If start is 0, the substring will start at the beginning of original.
  • If length is 0, the empty string will be returned.

Example

RETURN substring("Learning Cypher!", 2)

Returns the following Result

substring("Learning Cypher!", 2)
"arning Cypher!"

Example

RETURN substring("Learning Cypher!", 2, 7)

Returns the following Result

substring("Learning Cypher!", 2, 7)
"arning "

Example

RETURN substring(null, 2)

Returns the following Result

substring(null, 2)
null

Notesubstring(null, start) returns null.

Example

RETURN substring("Learning Cypher!", 2, 0)

Returns the following Result

substring("Learning Cypher!", 2, 0)
""

Example

RETURN substring("Learning Cypher!", 0, 8)

Returns the following Result

substring("Learning Cypher!", 0, 8)
"Learning"

Example

RETURN substring("Learning Cypher!", null, 7)

Returns the following Result

substring("Learning Cypher!", null, 7)
Error Neo.ClientError.Statement.TypeError

Note– Negative or Null values of start raises an error.

Example

RETURN substring("Learning Cypher!", -5, 7)

Returns the following Result

substring("Learning Cypher!", -5, 7)
Error Neo.DatabaseError.General.UnknownError

Note– Negative or Null values of start raises an error.

Example

RETURN substring("Learning Cypher!", 2, null)

Returns the following Result

substring("Learning Cypher!", 2, null)
Error Neo.ClientError.Statement.TypeError

Note– Negative or Null values of length gives an error.

Example

RETURN substring("Learning Cypher!", 2, -7)

Returns the following Result

substring("Learning Cypher!", 2, -7)
Error Neo.DatabaseError.General.UnknownError

Note– Negative or Null values of length gives an error.

Learn More