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

# `substr` 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` that starts at `pos` and is of length `len`. This function is a synonym for [`substring` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/substring.md).

## Syntax

```
substr(expr, pos [, len] )
```

```
substr(expr FROM pos[ FOR len])
```

## Arguments

* `expr`: An `BINARY` or `STRING` expression.
* `pos`: An integral numeric expression specifying the starting position.
* `len`: An optional integral numeric expression.

## Returns

The result matches the type of `expr`.

`pos` is 1 based. If `pos` is negative the start is determined by counting characters (or bytes for `BINARY`) from the end.

If `len` is less than 1 the result is empty.

If `len` is omitted the function returns on characters or bytes starting with `pos`.

## Examples

```sql
> SELECT substr('Spark SQL', 5);
 k SQL
> SELECT substr('Spark SQL', -3);
 SQL
> SELECT substr('Spark SQL', 5, 1);
 k
> SELECT substr('Spark SQL' FROM 5);
 k SQL
> SELECT substr('Spark SQL' FROM -3);
 SQL
> SELECT substr('Spark SQL' FROM 5 FOR 1);
 k
> SELECT substr('Spark SQL' FROM -10 FOR 5);
 Spar
```

## Related functions

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