Skip to main content

SUBSTRING function

Returns the substring of an expression that starts at a position and is of a certain length.

Syntax

SUBSTRING(expression, position, length)

Arguments

  • expression: The string.

  • position: The 1-based index from which the substring starts.

  • length: The length of the substring. If omitted, it will go until the end of the expression.

Returns

A VARCHAR.

Examples

> SELECT SUBSTRING('abc', 1);
  'abc'

> SELECT SUBSTRING('abc', 1, 1);
  'a'

> SELECT SUBSTRING('abc', 5);
  ''

Notes

Negative Position Behavior

  • Negative integer literals are not supported.

  • It is possible to have a negative position when using a function.

  • Vertica will start from the imaginary places before the actual start of the string.

    • Example: abc with a position of -1 and length of 3 returns a

  • Databricks will start counting backwards from the end of the string.

    • Example: abc with a positon of -2 and length of 2 returns bc

Negative Length Behavior

  • Negative integer literals are not supported.

  • It is possible to have a negative length when using a function.

  • Vertica will throw an exception.

  • Databricks will return an empty string.