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

# `array_insert` 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 13.3 LTS and above

Returns an expanded `array` where `elem` is inserted at the `index` position.

## Syntax

```
array_insert(array, index, elem)
```

## Arguments

* `array`: An ARRAY.
* `index`: A non-zero INTEGER expression specifying where to insert `elem`. If index is negative `elem` is inserted relative to the end of the array.
* `elem`: An expression of the same type as the elements of `array`.

## Returns

An ARRAY of the same type as `array`.

Databricks raises [INVALID\_INDEX\_OF\_ZERO](https://docs.databricks.com/aws/en/error-messages/error-classes.md#invalid_index_of_zero) if `index` is 0.

Databricks raises [COLLECTION\_SIZE\_LIMIT\_EXCEEDED](https://docs.databricks.com/aws/en/error-messages/collection-size-limit-exceeded-error-class.md) if the result exceeds the array size limit.

## Notes

All elements starting with `index` are shifted by one position to make space for `elem` at `index`.
If `index` is outside the cardinality of `array` the array is padded with `NULL`s.

## Common error conditions

* [COLLECTION\_SIZE\_LIMIT\_EXCEEDED](https://docs.databricks.com/aws/en/error-messages/collection-size-limit-exceeded-error-class.md)
* [INVALID\_INDEX\_OF\_ZERO](https://docs.databricks.com/aws/en/error-messages/error-classes.md#invalid_index_of_zero)

## Examples

```sql
> SELECT array_insert(array('a', 'b', 'c'), 1, 'z');
 ["z","a","b","c"]

> SELECT array_insert(array('a', 'b', 'c'), 0, 'z');
 Error: INVALID_INDEX_OF_ZERO

> SELECT array_insert(array('a', 'b', 'c'), -1, 'z');
 ["a","b","c","z"]

> SELECT array_insert(array('a', 'b', 'c'), 5, 'z');
 ["a","b","c",NULL,"z"]

> SELECT array_insert(array('a', 'b', 'c'), -5, 'z');
 ["z",NULL,"a","b","c"]

> SELECT array_insert(array('a', 'b', 'c'), 2, cast(NULL AS STRING));
 ["a",NULL,"b","c"]
```

## Related functions

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