`substring` extracts part of some text. This function is useful for cleaning up text (or any value with a [string data type](https://www.metabase.com/learn/databases/data-types-overview#examples-of-data-types)) that has a consistent format.
For example, `substring` should work well on strings like SKU numbers, ISO codes, and standardized email addresses.
`substring` extracts text by counting characters from left to right. If you need to extract text based on some more complicated logic, try [`regexextract`](../expressions-list.md#regexextract).
And if you only need to clean up extra whitespace around your text, you can use the [`trim`](../expressions-list.md#trim), [`lefttrim`](../expressions-list.md#lefttrim), or [`righttrim`](../expressions-list.md#righttrim) expressions instead.
## Related functions
This section covers functions and formulas that work the same way as the Metabase `substring` expression, with notes on how to choose the best option for your use case.
-[SQL](#sql)
-[Spreadsheets](#spreadsheets)
-[Python](#python)
### SQL
When you run a question using the [notebook editor](https://www.metabase.com/glossary/notebook_editor), Metabase will convert your graphical query settings (filters, summaries, etc.) into a query, and run that query against your database to get your results.
If our [sample data](#getting-a-substring-from-the-left) is stored in a SQL database:
```sql
SELECT
mission_id,
SUBSTRING(mission_id,9,3)ASagent
FROM
this_message_will_self_destruct;
```
is equivalent to the Metabase `substring` expression:
```
substring([Mission ID], 9, 3)
```
### Spreadsheets
If our [sample data](#getting-a-substring-from-the-left) is in a spreadsheet where "Mission ID" is in column A,
```
=mid(A2,9,3)
```
is the same as the Metabase `substring` expression:
```
substring([Mission ID], 9, 3)
```
### Python
Assuming the [sample data](#getting-a-substring-from-the-left) is in a dataframe column called `df`,
```
df['Agent'] = df['Mission ID'].str.slice(8, 11)
```
does the same thing as the Metabase `substring` expression: