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

# `substring` 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 [`substr` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/substr.md).

## Syntax

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

```
substring(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

A `STRING`.

`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 substring('Spark SQL', 5);
 k SQL
> SELECT substring('Spark SQL', -3);
 SQL
> SELECT substring('Spark SQL', 5, 1);
 k
> SELECT substring('Spark SQL' FROM 5);
 k SQL
> SELECT substring('Spark SQL' FROM -3);
 SQL
> SELECT substring('Spark SQL' FROM 5 FOR 1);
 k
> SELECT substring('Spark SQL' FROM -10 FOR 5);
 Spar
```

## Related functions

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