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

substring_index

The substring_index function is used to truncate the string str before the count-th occurrence of the separator. If count is positive, truncation starts from the left. If count is negative, truncation starts from the right.

Syntax

substring_index(string <str>, string <separator>, int <count>) 

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

str

Yes

STRING

The string to be truncated.

separator

Yes

STRING

A STRING type delimiter.

count

Yes

INT

Specifies the delimiter position.

Return Values

Returns STRING type.

If any input parameter is NULL, NULL is returned.

Example Code

Returns hello.world.

SELECT substring_index('hello.world.people', '.', 2);   

Returns world.people.

select substring_index('hello.world.people', '.', -2);