---
title: substring_index function
description: Learn the syntax of the substring\_index function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/substring_index
---

# `substring_index` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the substring of `expr` before the `count` occurrence of the delimiter `delim`.

## Syntax

```
substring_index(expr, delim, count)
```

## Arguments

* `expr`: A `STRING` or `BINARY` expression.
* `delim`: An expression matching the type of `expr` specifying the delimiter.
* `count`: An `INTEGER` expression to count the delimiters.

## Returns

The result matches the type of `expr`.

If `count` is positive, everything to the left of the final delimiter (counting from the left) is returned.

If `count` is negative, everything to the right of the final delimiter (counting from the right) is returned.

## Examples

```sql
> SELECT substring_index('www.apache.org', '.', 2);
 www.apache

> SELECT substring_index('555A66A777' COLLATE UTF8_BINARY, 'a', 2);
 555A66A777

> SELECT substring_index('555A66A777' COLLATE UTF8_LCASE, 'a', 2);
 555A66
```

## Related functions

* [`substr` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/substr.md)
* [`substring` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/substring.md)
