Updated on 2026-11-06 GMT+08:00

substr/substring

Returns a substring of str starting from start_position with a length of length. substr and substring have the same functionality.

Syntax

substr(string <str>, bigint <start_position>[, bigint <length>]) 

or

substring(string <str>, bigint <start_position>[, bigint <length>]) 

Parameters

Parameter

Mandatory

Type

Description

str

Yes

STRING

The string from which to extract the substring.

start_position

Yes

BIGINT

The starting position, starting from 1. A positive value starts from the left, and a negative value starts from the right.

length

No

BIGINT

The length of the substring. Must be greater than 0. When omitted, the substring to the end of str is returned.

Return Values

Returns a value of type STRING.

  • When length is omitted, the substring to the end of str is returned.
  • If str, start_position, or length is NULL, NULL is returned.

Example Code

Returns k SQL.

SELECT substr('Spark SQL', 5); 

Returns SQL.

SELECT substr('Spark SQL', -3); 

Returns k.

SELECT substr('Spark SQL', 5, 1);