# SUBSTRING\_INDEX

## Syntax

```sql
SUBSTRING_INDEX(str,delim,count)
```

## Description

Returns the substring from string *`str`* before count occurrences of the delimiter *`delim`*. 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. `SUBSTRING_INDEX()` performs a case-sensitive match when searching for *`delim`*.

If any argument is `NULL`, returns `NULL`.

For example:

```sql
SUBSTRING_INDEX('www.mariadb.org', '.', 2)
```

It means "Return all of the characters up to the 2nd occurrence of ."

## Examples

```sql
SELECT SUBSTRING_INDEX('www.mariadb.org', '.', 2);
+--------------------------------------------+
| SUBSTRING_INDEX('www.mariadb.org', '.', 2) |
+--------------------------------------------+
| www.mariadb                                |
+--------------------------------------------+

SELECT SUBSTRING_INDEX('www.mariadb.org', '.', -2);
+---------------------------------------------+
| SUBSTRING_INDEX('www.mariadb.org', '.', -2) |
+---------------------------------------------+
| mariadb.org                                 |
+---------------------------------------------+
```

## See Also

* [INSTR()](https://mariadb.com/docs/server/reference/sql-functions/string-functions/instr) - Returns the position of a string within a string
* [LOCATE()](https://mariadb.com/docs/server/reference/sql-functions/string-functions/locate) - Returns the position of a string within a string
* [SUBSTRING()](https://mariadb.com/docs/server/reference/sql-functions/string-functions/substring) - Returns a string based on position

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-functions/string-functions/substring_index.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
