split_part function (Databricks SQL)

Splits str around occurrences of delim and returns the partNum part.

Syntax

split_part(str, delim, partNum)

Arguments

  • str: A STRING expression to be split.

  • delimiter: A STRING expression serving as delimiter for the parts.

  • partNum: An INTEGER expression electing the part to be returned.

Returns

A STRING.

If partNum >= 1: The partNums part counting from the beginning of str will be returned.

If partNum <= -1: The abs(partNum)s part counting from the end of str will be returned.

partNum must not be 0. split_part returns an empty string if partNum is beyond the number of parts in str.

Examples

> SELECT '->' || split_part('Hello,world,!', ',', 1) || '<-';
  ->Hello<-

> SELECT '->' || split_part('Hello,world,!', ',', 2) || '<-';
  -><-

> SELECT '->' || split_part('Hello,world,!', ',', 100) || '<-';
  -><-

> SELECT '->' || split_part('Hello,world,!', ',', -2) || '<-';
  ->!<-

> SELECT '->' || split_part('Hello,world,!', ',', -100) || '<-';
  -><-

> SELECT '->' || split_part('', ',', 1) || '<-';
  -><-

> SELECT '->' || split_part('Hello', '', 3) || '<-';
  -><-

> SELECT '->' || split_part('Hello,World,!', ',', 0) || '<-';
  ERROR: Index out of bound