

# General string functions

Use these functions for everyday work with strings, such as joining and splitting them, taking a substring, finding a substring's position, changing case, trimming, padding, and replacing text.

To count the characters in a string, use the [`length` function](duckdb-functions-list-length-and-elements.md#athena-duckdb-functions-list-length). To test whether a string starts with, ends with, or contains another string, see [Pattern matching functions and operators](duckdb-functions-string-pattern-matching.md).

## `||` (concatenation) operator
<a name="athena-duckdb-functions-string-concatenation-operator"></a>

Joins two values end to end. With two strings, returns `expression2` appended to `expression1`. With two lists, returns one list that holds the elements of both lists, in order. With two `BLOB` values, returns the bytes of both values as one `BLOB`.

If either operand is `NULL`, the result is `NULL`. To skip `NULL` values instead, use the [`concat` function](#athena-duckdb-functions-string-concat).

When the operands are not both lists or both `BLOB` values, they are converted to text, so `'order-' || 42` returns `order-42`. A list can be concatenated only with another list.

### Syntax
<a name="athena-duckdb-functions-string-concatenation-operator-syntax"></a>

```sql
{{expression1}} || {{expression2}}
```

### Arguments
<a name="athena-duckdb-functions-string-concatenation-operator-arguments"></a>

The `||` (concatenation) operator takes the following arguments:

**{{expression1}}**  
The first value to join: a `VARCHAR` string, a list, a `BLOB`, or a value that can be converted to text.

**{{expression2}}**  
The value to append. If either operand is a list, both operands must be lists. To get a `BLOB` result, both operands must be `BLOB` values. Otherwise, as with `expression1`, any value that can be converted to text works.

### Return type
<a name="athena-duckdb-functions-string-concatenation-operator-return-type"></a>

`VARCHAR`. When both operands are lists, the result is a list type, such as `INTEGER[]`. When both operands are `BLOB` values, the result is a `BLOB`.

### Examples
<a name="athena-duckdb-functions-string-concatenation-operator-examples"></a>

The following SQL query concatenates two strings.

```sql
SELECT 'Duck' || 'DB' AS result
```

The query returns the following result.


| result | 
| --- | 
| `DuckDB` | 

The following SQL query concatenates two lists.

```sql
SELECT [1, 2, 3] || [4, 5, 6] AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, 4, 5, 6]` | 

The following SQL query concatenates two one-byte `BLOB` values that are written as hexadecimal escapes.

```sql
SELECT '\xAA'::BLOB || '\xBB'::BLOB AS result
```

The query returns the following result.


| result | 
| --- | 
| `\xAA\xBB` | 

The following SQL query concatenates a string with a number, and a string with `NULL`.

```sql
SELECT
    'order-' || 42 AS number_operand,
    'Duck' || NULL AS null_operand
```

The query returns the following result.


| number\_operand | null\_operand | 
| --- | --- | 
| `order-42` | `NULL` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-concatenation-operator-differences-from-engine-version-3"></a>

In Athena engine version 3, `||` with a number operand fails unless you cast the number to `VARCHAR`. In DuckDB, the number is converted to text, so `'order-' || 42` returns `order-42`.

## `concat` function
<a name="athena-duckdb-functions-string-concat"></a>

Joins the arguments in order and returns the result. Arguments that are not strings, such as numbers and dates, are converted to text, and the result is a `VARCHAR`. If the arguments are lists, returns one list that holds the elements of every list, in order.

`concat` skips `NULL` arguments. If every argument is `NULL`, returns an empty string, or an empty list when the arguments are lists. It does not return `NULL`. To get `NULL` when any part is `NULL`, use the `||` operator.

### Syntax
<a name="athena-duckdb-functions-string-concat-syntax"></a>

```sql
concat({{value}} [, ...])
```

### Arguments
<a name="athena-duckdb-functions-string-concat-arguments"></a>

The `concat` function takes the following argument:

**{{value}}**  
One or more values to join. Strings and other scalar values are joined as text. If the values are lists, their element types must be compatible.

### Return type
<a name="athena-duckdb-functions-string-concat-return-type"></a>

`VARCHAR`, or a list type such as `INTEGER[]` when the arguments are lists.

### Examples
<a name="athena-duckdb-functions-string-concat-examples"></a>

The following SQL query joins three strings.

```sql
SELECT concat('Hello', ' ', 'World') AS result
```

The query returns the following result.


| result | 
| --- | 
| `Hello World` | 

The following SQL query compares `concat` with the `||` operator when one part is `NULL`.

```sql
SELECT
    concat('a', NULL, 'b') AS concat_result,
    'a' || NULL || 'b' AS operator_result
```

The query returns the following result.


| concat\_result | operator\_result | 
| --- | --- | 
| `ab` | `NULL` | 

The following SQL query shows that `concat` of only `NULL` arguments returns an empty string, and that a number is joined as text.

```sql
SELECT
    concat(NULL, NULL) = '' AS all_null_is_empty,
    concat('order-', 42) AS with_number
```

The query returns the following result.


| all\_null\_is\_empty | with\_number | 
| --- | --- | 
| `true` | `order-42` | 

The following SQL query joins two lists and skips a `NULL` argument.

```sql
SELECT concat([1, 2, 3], NULL, [4, 5, 6]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, 4, 5, 6]` | 

### Considerations
<a name="athena-duckdb-functions-string-concat-considerations"></a>

`concat` converts `BLOB` arguments to text, so it returns a `VARCHAR`. To join `BLOB` values and keep the `BLOB` type, use the `||` operator.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-concat-differences-from-engine-version-3"></a>

In Athena engine version 3, `concat` returns `NULL` when any argument is `NULL`. In DuckDB, `concat` skips `NULL` arguments, so `concat('a', NULL, 'b')` returns `ab`. To get the Athena engine version 3 result, use the `||` operator, which returns `NULL` when either operand is `NULL`.

## `concat_ws` function
<a name="athena-duckdb-functions-string-concat-ws"></a>

Returns the arguments that follow `separator` joined into one `VARCHAR`, with `separator` between each pair. Arguments that are not strings, such as numbers, are converted to text.

`NULL` arguments are skipped, and no separator is added for them. An empty string is not `NULL`, so it is kept and gets a separator. If `separator` is `NULL`, returns `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-concat-ws-syntax"></a>

```sql
concat_ws({{separator}}, {{value}} [, ...])
```

### Arguments
<a name="athena-duckdb-functions-string-concat-ws-arguments"></a>

The `concat_ws` function takes the following arguments:

**{{separator}}**  
The separator to put between the joined values, as a `VARCHAR` value.

**{{value}}**  
One or more values to join, of any type.

### Return type
<a name="athena-duckdb-functions-string-concat-ws-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-concat-ws-examples"></a>

The following SQL query joins three strings with a comma and a space.

```sql
SELECT
    concat_ws(
        ', ', 'Banana', 'Apple', 'Melon'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Banana, Apple, Melon` | 

The following SQL query shows that a `NULL` argument is skipped, but an empty string is kept.

```sql
SELECT
    concat_ws(
        ', ', 'Banana', NULL, 'Melon'
    ) AS skips_null,
    concat_ws('-', '', 'b') AS keeps_empty_string
```

The query returns the following result.


| skips\_null | keeps\_empty\_string | 
| --- | --- | 
| `Banana, Melon` | `-b` | 

The following SQL query passes a `NULL` separator, and joins numbers.

```sql
SELECT
    concat_ws(NULL, 'a', 'b') AS null_separator,
    concat_ws('-', 2026, 10, 1) AS numbers
```

The query returns the following result.


| null\_separator | numbers | 
| --- | --- | 
| `NULL` | `2026-10-1` | 

## `instr` function
<a name="athena-duckdb-functions-string-instr"></a>

Finds `search_string` in `string` and returns the 1-based position at which its first occurrence starts. Positions count Unicode code points, not bytes. The search is case-sensitive. If `search_string` does not occur in `string`, the result is `0`. If `search_string` is an empty string, the result is `1`. If either argument is `NULL`, the result is `NULL`.

`strpos` is an alias of `instr`. Another alias, `position`, uses the SQL standard form `position({{search_string}} IN {{string}})`, with the search string first. The `position` alias does not accept a comma between its arguments.

### Syntax
<a name="athena-duckdb-functions-string-instr-syntax"></a>

```sql
instr({{string}}, {{search_string}})
strpos({{string}}, {{search_string}})
position({{search_string}} IN {{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-instr-arguments"></a>

The `instr` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{search\_string}}**  
The string to find, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-instr-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-string-instr-examples"></a>

The following SQL query finds the first occurrence of `es`.

```sql
SELECT instr('test test', 'es') AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query uses the `strpos` alias with a search string that does not occur, so the result is `0`, not `NULL`.

```sql
SELECT strpos('test test', 'x') AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

The following SQL query uses the `position` alias with the `IN` form, which puts the search string first. The letter `b` starts at position 2 of `abc`.

```sql
SELECT position('b' IN 'abc') AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query builds a string that starts with a character that takes two bytes in UTF-8 (code point 228). It shows that the position counts code points, not bytes.

```sql
SELECT
    instr(chr(228) || 'b', 'b') AS char_position,
    strlen(chr(228) || 'b') AS byte_length
```

The query returns the following result.


| char\_position | byte\_length | 
| --- | --- | 
| `2` | `3` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-instr-differences-from-engine-version-3"></a>

In Athena engine version 3, `strpos` accepts an optional third argument, `instance`, that selects which occurrence to find. In DuckDB, `strpos` and `instr` take exactly two arguments and always return the first occurrence.

## `left` function
<a name="athena-duckdb-functions-string-left"></a>

Returns the first `count` characters of `string`. If `count` is greater than the length of `string`, the result is the whole string. If `count` is negative, the function drops that many characters from the end of `string` and returns the rest. If either argument is `NULL`, the result is `NULL`.

`left` measures `count` in Unicode code points, not bytes. To count characters as a reader sees them, such as a letter followed by a combining accent, use the [`left_grapheme` function](duckdb-functions-string-unicode-characters.md#athena-duckdb-functions-string-left-grapheme).

### Syntax
<a name="athena-duckdb-functions-string-left-syntax"></a>

```sql
left({{string}}, {{count}})
```

### Arguments
<a name="athena-duckdb-functions-string-left-arguments"></a>

The `left` function takes the following arguments:

**{{string}}**  
The string to take characters from, as a `VARCHAR` value.

**{{count}}**  
The number of characters to return, as a `BIGINT` value. A negative value drops that many characters from the end instead.

### Return type
<a name="athena-duckdb-functions-string-left-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-left-examples"></a>

The following SQL query returns the first two characters of a string.

```sql
SELECT left('Hello', 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `He` | 

The following SQL query passes a negative count, which drops the last two characters.

```sql
SELECT left('Hello', -2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Hel` | 

The following SQL query passes a count that is greater than the length of the string, so the whole string is returned.

```sql
SELECT left('Hello', 10) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Hello` | 

### Considerations
<a name="athena-duckdb-functions-string-left-considerations"></a>

`left` takes a string. To take the leftmost digits of a number, cast the number first, for example `left(CAST(12345 AS VARCHAR), 2)`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-left-differences-from-engine-version-3"></a>

Athena engine version 3 does not have a `left` function. To write a query that runs on both engines, use `substr({{string}}, 1, {{count}})`.

In Athena engine version 3, `CAST({{x}} AS VARCHAR({{n}}))` truncates the value to `n` characters. DuckDB ignores the length in `VARCHAR({{n}})`, so use `left({{x}}, {{n}})` to truncate a string.

## `lower` function
<a name="athena-duckdb-functions-string-lower"></a>

Returns `string` with each uppercase letter changed to its lowercase form. Letters outside the ASCII range are converted too, and characters that have no case, such as digits and punctuation, are returned unchanged. If `string` is `NULL`, the result is `NULL`.

`lcase` is an alias of `lower`. To convert a string to uppercase, use the [`upper` function](#athena-duckdb-functions-string-upper).

### Syntax
<a name="athena-duckdb-functions-string-lower-syntax"></a>

```sql
lower({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-lower-arguments"></a>

The `lower` function takes the following argument:

**{{string}}**  
The string to convert, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-lower-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-lower-examples"></a>

The following SQL query converts a string to lowercase.

```sql
SELECT lower('Hello') AS result
```

The query returns the following result.


| result | 
| --- | 
| `hello` | 

The following SQL query uses the `lcase` alias.

```sql
SELECT lcase('DuckDB') AS result
```

The query returns the following result.


| result | 
| --- | 
| `duckdb` | 

The following SQL query converts a letter outside the ASCII range. `chr(196)` is the uppercase letter A with a diaeresis, and `chr(228)` is the same letter in lowercase.

```sql
SELECT lower(chr(196)) = chr(228) AS is_converted
```

The query returns the following result.


| is\_converted | 
| --- | 
| `true` | 

## `lpad` function
<a name="athena-duckdb-functions-string-lpad"></a>

Returns `string` padded on the left with `character` until the result is `count` characters long. If `character` has more than one character, the function repeats it and cuts the last repeat to fit.

If `string` is already longer than `count` characters, the function returns only its first `count` characters, so characters are removed from the right, not the left. If `count` is `0` or negative, the result is an empty string. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-lpad-syntax"></a>

```sql
lpad({{string}}, {{count}}, {{character}})
```

### Arguments
<a name="athena-duckdb-functions-string-lpad-arguments"></a>

The `lpad` function takes the following arguments:

**{{string}}**  
The string to pad, as a `VARCHAR` value.

**{{count}}**  
The length of the result, in characters, as an `INTEGER` value.

**{{character}}**  
The text to pad with, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-lpad-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-lpad-examples"></a>

The following SQL query pads a string on the left with `>` characters to a length of 8.

```sql
SELECT lpad('hello', 8, '>') AS result
```

The query returns the following result.


| result | 
| --- | 
| `>>>hello` | 

The following SQL query pads a number with leading zeros to three digits. Because `lpad` pads only `VARCHAR` values, the query casts the number to `VARCHAR` first.

```sql
SELECT lpad(CAST(7 AS VARCHAR), 3, '0') AS result
```

The query returns the following result.


| result | 
| --- | 
| `007` | 

The following SQL query asks for a length that is shorter than the string, so the function returns the first three characters.

```sql
SELECT lpad('hello', 3, '>') AS result
```

The query returns the following result.


| result | 
| --- | 
| `hel` | 

### Considerations
<a name="athena-duckdb-functions-string-lpad-considerations"></a>

`lpad` does not convert a number to a string for you. A call such as `lpad(7, 3, '0')` fails, so cast the number first, as in `lpad(CAST(7 AS VARCHAR), 3, '0')`.

If `character` is an empty string and `string` is shorter than `count`, the query fails.

`count` must be an `INTEGER`. A `BIGINT` value, such as the result of `length`, makes the query fail. Cast it to `INTEGER` first, as in `lpad('a', CAST(length('abc') AS INTEGER), '0')`.

## `ltrim` function
<a name="athena-duckdb-functions-string-ltrim"></a>

Returns `string` with characters removed from its start. With one argument, the function removes leading spaces. With `characters`, it removes each leading character that appears anywhere in `characters`, in any order, and stops at the first character that does not.

If any argument is `NULL`, the result is `NULL`. To remove characters from the end of a string, use the [`rtrim` function](#athena-duckdb-functions-string-rtrim). To remove them from both ends, use the [`trim` function](#athena-duckdb-functions-string-trim).

### Syntax
<a name="athena-duckdb-functions-string-ltrim-syntax"></a>

```sql
ltrim({{string}} [, {{characters}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-ltrim-arguments"></a>

The `ltrim` function takes the following arguments:

**{{string}}**  
The string to trim, as a `VARCHAR` value.

**{{characters}}**  
The characters to remove, listed in a `VARCHAR` value. The function treats it as a set of single characters, not as a substring. If you omit it, `ltrim` removes leading space characters, including Unicode spaces such as the no-break space. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-ltrim-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-ltrim-examples"></a>

The following SQL query removes the leading spaces from a string.

```sql
SELECT ltrim('   test') AS result
```

The query returns the following result.


| result | 
| --- | 
| `test` | 

The following SQL query removes every leading `>` and `<` character. The characters at the end of the string are kept.

```sql
SELECT ltrim('>>>>test<<', '><') AS result
```

The query returns the following result.


| result | 
| --- | 
| `test<<` | 

The following SQL query shows that `ltrim` with one argument keeps a leading tab, `chr(9)`. The trimmed string still has four characters: the tab and `abc`.

```sql
SELECT length(ltrim(chr(9) || 'abc')) AS result
```

The query returns the following result.


| result | 
| --- | 
| `4` | 

The following SQL query removes a leading tab, line break, and space by listing them in `characters`.

```sql
SELECT
    ltrim(
        chr(9) || chr(10) || ' abc',
        ' ' || chr(9) || chr(10) || chr(13)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `abc` | 

### Considerations
<a name="athena-duckdb-functions-string-ltrim-considerations"></a>

With one argument, `ltrim` removes leading space characters, including Unicode spaces such as the no-break space (`chr(160)`), but not tabs, line breaks, or other whitespace. To remove those too, list them in `characters`, for example `ltrim({{string}}, ' ' || chr(9) || chr(10) || chr(13))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-ltrim-differences-from-engine-version-3"></a>

In Athena engine version 3, `ltrim({{string}})` also removes leading tabs and line breaks. In DuckDB, it removes only space characters. To get the Athena engine version 3 result, pass the whitespace characters that you want to remove, for example `ltrim({{string}}, ' ' || chr(9) || chr(10) || chr(13))`.

## `overlay` function
<a name="athena-duckdb-functions-string-overlay"></a>

Returns `string` with `count` characters, starting at position `start`, replaced by `replacement`. The first character of `string` is at position `1`. Without `count`, the function replaces as many characters as `replacement` has.

Use a `count` of `0` to insert `replacement` without removing any characters, or an empty `replacement` to remove characters. If `start` is past the end of `string`, `replacement` is added to the end. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-overlay-syntax"></a>

```sql
overlay({{string}} PLACING {{replacement}} FROM {{start}}
    [ FOR {{count}} ])
overlay({{string}}, {{replacement}}, {{start}} [, {{count}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-overlay-arguments"></a>

The `overlay` function takes the following arguments:

**{{string}}**  
The string to change, as a `VARCHAR` value.

**{{replacement}}**  
The text to put into `string`, as a `VARCHAR` value.

**{{start}}**  
The position of the first character to replace, as a `BIGINT` value. The first character of `string` is at position `1`.

**{{count}}**  
The number of characters to replace, as a `BIGINT` value. The default is the length of `replacement`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-overlay-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-overlay-examples"></a>

The following SQL query replaces three characters, starting at position 2.

```sql
SELECT
    overlay(
        'hello' PLACING 'xyz' FROM 2 FOR 3
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `hxyzo` | 

The following SQL query uses a `count` of `0` to insert a string at position 3 without removing any characters.

```sql
SELECT
    overlay('hello' PLACING 'XY' FROM 3 FOR 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `heXYllo` | 

The following SQL query uses the comma-separated form with an empty replacement to remove three characters, starting at position 2.

```sql
SELECT overlay('hello', '', 2, 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `ho` | 

The following SQL query starts past the end of the string, so the replacement is added to the end.

```sql
SELECT overlay('abc' PLACING 'Z' FROM 10) AS result
```

The query returns the following result.


| result | 
| --- | 
| `abcZ` | 

## `replace` function
<a name="athena-duckdb-functions-string-replace"></a>

Returns `string` with every occurrence of `source` replaced by `target`. The search is case-sensitive, and `source` is literal text, not a pattern. To replace a regular expression match, use `regexp_replace`.

If `source` is an empty string, returns `string` unchanged.

### Syntax
<a name="athena-duckdb-functions-string-replace-syntax"></a>

```sql
replace({{string}}, {{source}}, {{target}})
```

### Arguments
<a name="athena-duckdb-functions-string-replace-arguments"></a>

The `replace` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{source}}**  
The text to find, as a `VARCHAR` value.

**{{target}}**  
The text that replaces each occurrence of `source`, as a `VARCHAR` value. To remove `source`, pass `''`.

### Return type
<a name="athena-duckdb-functions-string-replace-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-replace-examples"></a>

The following SQL query replaces each `l` with a hyphen.

```sql
SELECT replace('hello', 'l', '-') AS result
```

The query returns the following result.


| result | 
| --- | 
| `he--o` | 

The following SQL query returns the string unchanged, because the search is case-sensitive and `h` does not match `H`.

```sql
SELECT replace('Hello', 'h', 'J') AS result
```

The query returns the following result.


| result | 
| --- | 
| `Hello` | 

The following SQL query passes an empty `source`, so it returns the string unchanged.

```sql
SELECT replace('abc', '', 'x') AS result
```

The query returns the following result.


| result | 
| --- | 
| `abc` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-replace-differences-from-engine-version-3"></a>

In Athena engine version 3, you can call `replace` with two arguments to remove every occurrence of `source`. DuckDB requires all three arguments. To remove text, pass `''` as `target`.

In Athena engine version 3, an empty `source` inserts `target` before every character and at the end, so `replace('abc', '', 'x')` returns `xaxbxcx`. In DuckDB, an empty `source` leaves the string unchanged.

## `reverse` function
<a name="athena-duckdb-functions-string-reverse"></a>

Returns `string` with its characters in reverse order. A character and a combining mark that follows it, such as an accent, stay together, so the mark still follows its character in the result.

### Syntax
<a name="athena-duckdb-functions-string-reverse-syntax"></a>

```sql
reverse({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-reverse-arguments"></a>

The `reverse` function takes the following argument:

**{{string}}**  
The string to reverse, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-reverse-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-reverse-examples"></a>

The following SQL query reverses a string.

```sql
SELECT reverse('hello') AS result
```

The query returns the following result.


| result | 
| --- | 
| `olleh` | 

The following SQL query reverses `cafe` followed by a combining acute accent (`chr(769)`). The accent stays after the `e`.

```sql
SELECT
    reverse(
        'cafe' || chr(769)
    ) = 'e' || chr(769) || 'fac' AS accent_stays_with_e
```

The query returns the following result.


| accent\_stays\_with\_e | 
| --- | 
| `true` | 

## `right` function
<a name="athena-duckdb-functions-string-right"></a>

Returns the last `count` characters of `string`. If `count` is greater than the length of `string`, returns the whole string.

If `count` is negative, removes that many characters from the start of `string` and returns the rest.

`right` counts Unicode code points. To count what a reader sees as one character, such as a letter with a combining accent, use the [`right_grapheme` function](duckdb-functions-string-unicode-characters.md#athena-duckdb-functions-string-right-grapheme).

### Syntax
<a name="athena-duckdb-functions-string-right-syntax"></a>

```sql
right({{string}}, {{count}})
```

### Arguments
<a name="athena-duckdb-functions-string-right-arguments"></a>

The `right` function takes the following arguments:

**{{string}}**  
The string to take characters from, as a `VARCHAR` value.

**{{count}}**  
The number of characters to return, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-string-right-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-right-examples"></a>

The following SQL query returns the last three characters of a string.

```sql
SELECT right('Hello', 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `llo` | 

The following SQL query passes a `count` that is greater than the length of the string, and a negative `count`, which removes two characters from the start.

```sql
SELECT
    right('Hello', 10) AS count_too_large,
    right('Hello', -2) AS negative_count
```

The query returns the following result.


| count\_too\_large | negative\_count | 
| --- | --- | 
| `Hello` | `llo` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-right-differences-from-engine-version-3"></a>

Athena engine version 3 does not have a `right` function. To write a query that runs on both engines, use `substr({{string}}, -{{count}})` with a `count` from `1` to the length of `string`.

## `rpad` function
<a name="athena-duckdb-functions-string-rpad"></a>

Returns `string` padded on the right with `character` until it is `count` characters long. If `string` is already longer than `count`, returns its first `count` characters.

`character` can be longer than one character. The padding repeats it and cuts the last repeat to fit.

### Syntax
<a name="athena-duckdb-functions-string-rpad-syntax"></a>

```sql
rpad({{string}}, {{count}}, {{character}})
```

### Arguments
<a name="athena-duckdb-functions-string-rpad-arguments"></a>

The `rpad` function takes the following arguments:

**{{string}}**  
The string to pad, as a `VARCHAR` value.

**{{count}}**  
The length of the result, in characters, as an `INTEGER` value.

**{{character}}**  
The text to pad with, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-rpad-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-rpad-examples"></a>

The following SQL query pads a string to 10 characters.

```sql
SELECT rpad('hello', 10, '<') AS result
```

The query returns the following result.


| result | 
| --- | 
| `hello<<<<<` | 

The following SQL query passes a `count` that is less than the length of the string, so the result is truncated.

```sql
SELECT rpad('hello', 3, '<') AS result
```

The query returns the following result.


| result | 
| --- | 
| `hel` | 

The following SQL query pads with a two-character string.

```sql
SELECT rpad('abc', 7, 'xy') AS result
```

The query returns the following result.


| result | 
| --- | 
| `abcxyxy` | 

### Considerations
<a name="athena-duckdb-functions-string-rpad-considerations"></a>

If `character` is an empty string and `string` is shorter than `count`, the query fails.

## `rtrim` function
<a name="athena-duckdb-functions-string-rtrim"></a>

Returns `string` with trailing characters removed. With one argument, removes trailing spaces. With `characters`, removes every trailing character that appears anywhere in `characters`, in any order.

### Syntax
<a name="athena-duckdb-functions-string-rtrim-syntax"></a>

```sql
rtrim({{string}} [, {{characters}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-rtrim-arguments"></a>

The `rtrim` function takes the following arguments:

**{{string}}**  
The string to trim, as a `VARCHAR` value.

**{{characters}}**  
The characters to remove, listed in a `VARCHAR` value. If you omit it, `rtrim` removes trailing space characters, including Unicode spaces such as the no-break space. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-rtrim-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-rtrim-examples"></a>

The following SQL query removes the trailing spaces. The `|` shows where the result ends.

```sql
SELECT rtrim('test   ') || '|' AS result
```

The query returns the following result.


| result | 
| --- | 
| `test\|` | 

The following SQL query removes trailing `>` and `<` characters. The leading `>` characters stay.

```sql
SELECT rtrim('>>>>test<<', '><') AS result
```

The query returns the following result.


| result | 
| --- | 
| `>>>>test` | 

The following SQL query shows that `rtrim` with one argument does not remove a trailing tab (`chr(9)`), so the result still has five characters.

```sql
SELECT length(rtrim('test' || chr(9))) AS result
```

The query returns the following result.


| result | 
| --- | 
| `5` | 

### Considerations
<a name="athena-duckdb-functions-string-rtrim-considerations"></a>

With one argument, `rtrim` removes trailing space characters, including Unicode spaces such as the no-break space (`chr(160)`), but not tabs, newlines, or carriage returns. To remove those too, list them in `characters`, for example `rtrim({{string}}, ' ' || chr(9) || chr(10) || chr(13))`. With `characters`, `rtrim` removes only the characters that you list.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-rtrim-differences-from-engine-version-3"></a>

In Athena engine version 3, `rtrim` with one argument also removes trailing tabs and newlines. In DuckDB, it removes only space characters. To get the Athena engine version 3 result for tabs, newlines, and carriage returns, use `rtrim({{string}}, ' ' || chr(9) || chr(10) || chr(13))`.

In Athena engine version 3, `rtrim` with one argument keeps a trailing no-break space (`chr(160)`). In DuckDB, it removes it.

## `split_part` function
<a name="athena-duckdb-functions-string-split-part"></a>

Splits `string` at each occurrence of `delimiter` and returns the part at `position`, counting from `1`. A negative `position` counts from the end, so `-1` returns the last part.

If `position` is past the last part, or is `0`, returns an empty string. If any argument is `NULL`, returns `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-split-part-syntax"></a>

```sql
split_part({{string}}, {{delimiter}}, {{position}})
```

### Arguments
<a name="athena-duckdb-functions-string-split-part-arguments"></a>

The `split_part` function takes the following arguments:

**{{string}}**  
The string to split, as a `VARCHAR` value.

**{{delimiter}}**  
The text that separates the parts, as a `VARCHAR` value. It is matched literally, not as a pattern.

**{{position}}**  
The position of the part to return, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-string-split-part-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-split-part-examples"></a>

The following SQL query splits a string at each semicolon and returns the second part.

```sql
SELECT split_part('a;b;c', ';', 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `b` | 

The following SQL query asks for a part past the last one, so it returns an empty string, not `NULL`.

```sql
SELECT split_part('a;b;c', ';', 5) = '' AS is_empty
```

The query returns the following result.


| is\_empty | 
| --- | 
| `true` | 

The following SQL query passes a negative `position` to return the last part.

```sql
SELECT split_part('a;b;c', ';', -1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `c` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-split-part-differences-from-engine-version-3"></a>

In Athena engine version 3, `split_part` returns `NULL` when `position` is past the last part. In DuckDB, it returns an empty string. To get the Athena engine version 3 result, use `NULLIF(split_part({{string}}, {{delimiter}}, {{position}}), '')`.

## `string_split` function
<a name="athena-duckdb-functions-string-string-split"></a>

Splits `string` at each occurrence of `separator` and returns the parts as a list. `separator` is matched literally, not as a regular expression. To split at a pattern, use the [`string_split_regex` function](duckdb-functions-string-regular-expressions.md#athena-duckdb-functions-string-string-split-regex).

If `separator` is an empty string, returns a list of the individual characters of `string`. If `string` is an empty string, returns a list with one empty element.

`split`, `str_split`, and `string_to_array` are aliases of `string_split`.

### Syntax
<a name="athena-duckdb-functions-string-string-split-syntax"></a>

```sql
string_split({{string}}, {{separator}})
```

### Arguments
<a name="athena-duckdb-functions-string-string-split-arguments"></a>

The `string_split` function takes the following arguments:

**{{string}}**  
The string to split, as a `VARCHAR` value.

**{{separator}}**  
The text that separates the parts, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-string-split-return-type"></a>

`VARCHAR[]`

### Examples
<a name="athena-duckdb-functions-string-string-split-examples"></a>

The following SQL query splits a string at each hyphen.

```sql
SELECT string_split('hello-world', '-') AS result
```

The query returns the following result.


| result | 
| --- | 
| `[hello, world]` | 

The following SQL query returns the second part. List positions start at `1`.

```sql
SELECT string_split('hello-world', '-')[2] AS result
```

The query returns the following result.


| result | 
| --- | 
| `world` | 

The following SQL query splits an empty string. The result is a list with one empty element, not an empty list.

```sql
SELECT len(string_split('', ',')) AS part_count
```

The query returns the following result.


| part\_count | 
| --- | 
| `1` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-string-split-differences-from-engine-version-3"></a>

In Athena engine version 3, `split` accepts an optional third argument that limits the number of parts. In DuckDB, `string_split` and its aliases take only `string` and `separator`.

## `substring` function
<a name="athena-duckdb-functions-string-substring"></a>

Returns the characters of `string` from position `start` to the end of the string. With `length`, returns at most `length` characters, beginning at position `start`. Positions count characters, not bytes, and position `1` is the first character.

A negative `start` counts back from the end of the string, so `-1` is the last character. A `start` of `0` is the position immediately before the first character. A `start` past the end of the string returns an empty string. If any argument is `NULL`, the result is `NULL`.

`substr` is an alias of `substring`. You can also use the SQL standard form, with the keywords `FROM` and `FOR`.

### Syntax
<a name="athena-duckdb-functions-string-substring-syntax"></a>

```sql
substring({{string}}, {{start}} [, {{length}} ])
substring({{string}} FROM {{start}} [ FOR {{length}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-substring-arguments"></a>

The `substring` function takes the following arguments:

**{{string}}**  
The string to take characters from, as a `VARCHAR` value.

**{{start}}**  
The position of the first character to return, as a `BIGINT` value. Position `1` is the first character, and a negative value counts back from the end of the string.

**{{length}}**  
The number of characters to return, as a `BIGINT` value. If you omit it, the result runs to the end of the string. A negative value returns up to that many characters before `start` instead, so `substring('Hello', 3, -1)` returns `e`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-substring-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-substring-examples"></a>

The following SQL query returns two characters, starting at the second character.

```sql
SELECT substring('Hello', 2, 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `el` | 

The following SQL query uses the `substr` alias with a negative start position to return the last three characters.

```sql
SELECT substr('Hello', -3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `llo` | 

The following SQL query uses the SQL standard form to return three characters, starting at the second character.

```sql
SELECT substring('Hello' FROM 2 FOR 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `ell` | 

The following SQL query shows two edge cases. With a start position of `0`, the position before the first character counts as one of the two characters, so the result is only `H`. A start position past the end of the string returns an empty string, not `NULL`.

```sql
SELECT
    substring('Hello', 0, 2) AS start_zero,
    substring('Hello', 10) = '' AS past_end_is_empty
```

The query returns the following result.


| start\_zero | past\_end\_is\_empty | 
| --- | --- | 
| `H` | `true` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-substring-differences-from-engine-version-3"></a>

In Athena engine version 3, `substr({{string}}, 0, {{length}})` returns an empty string. In DuckDB, position `0` is the position immediately before the first character, so the result holds one character fewer than `length`. For example, `substring('Hello', 0, 2)` returns `H`.

## `translate` function
<a name="athena-duckdb-functions-string-translate"></a>

Returns `string` with each character that appears in `from` replaced by the character at the same position in `to`. When `from` is longer than `to`, the characters of `from` that have no partner in `to` are removed from the result. Characters that do not appear in `from` are unchanged.

`translate` works one character at a time. To replace a substring, use the [`replace` function](#athena-duckdb-functions-string-replace). If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-translate-syntax"></a>

```sql
translate({{string}}, {{from}}, {{to}})
```

### Arguments
<a name="athena-duckdb-functions-string-translate-arguments"></a>

The `translate` function takes the following arguments:

**{{string}}**  
The string to change, as a `VARCHAR` value.

**{{from}}**  
The characters to replace or remove, listed in a `VARCHAR` value.

**{{to}}**  
The replacement characters, listed in a `VARCHAR` value in the same order as the characters in `from`.

### Return type
<a name="athena-duckdb-functions-string-translate-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-translate-examples"></a>

The following SQL query replaces `1` with `a` and `4` with `x`. It removes `3`, because `to` has no third character.

```sql
SELECT translate('12345', '143', 'ax') AS result
```

The query returns the following result.


| result | 
| --- | 
| `a2x5` | 

The following SQL query removes every hyphen by passing an empty string as `to`.

```sql
SELECT translate('2026-11-18', '-', '') AS result
```

The query returns the following result.


| result | 
| --- | 
| `20261118` | 

### Considerations
<a name="athena-duckdb-functions-string-translate-considerations"></a>

If a character appears more than once in `from`, its first position decides what replaces it.

## `trim` function
<a name="athena-duckdb-functions-string-trim"></a>

Returns `string` with its leading and trailing spaces removed. With `characters`, removes every leading and trailing character that appears in `characters`, in any order, instead of spaces.

Without `characters`, `trim` removes only space characters: the ordinary space and the other Unicode space separators, such as the no-break space `chr(160)`. Tabs, line breaks, and other whitespace characters that are not space separators stay in the result. If `string` is `NULL`, the result is `NULL`.

To remove characters from only the start or only the end of a string, use the [`ltrim` function](#athena-duckdb-functions-string-ltrim) or the [`rtrim` function](#athena-duckdb-functions-string-rtrim). You can also use the SQL standard form with `LEADING` or `TRAILING`.

### Syntax
<a name="athena-duckdb-functions-string-trim-syntax"></a>

```sql
trim({{string}} [, {{characters}} ])
trim([ BOTH | LEADING | TRAILING ]
    [ {{characters}} ] FROM {{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-trim-arguments"></a>

The `trim` function takes the following arguments:

**{{string}}**  
The string to trim, as a `VARCHAR` value.

**{{characters}}**  
The characters to remove, listed in a `VARCHAR` value. The `trim` function removes each character in it on its own, not as a sequence. If you omit it, `trim` removes space characters, including the no-break space `chr(160)`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-trim-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-trim-examples"></a>

The following SQL query removes the spaces from both ends of a string.

```sql
SELECT trim('    test  ') AS result
```

The query returns the following result.


| result | 
| --- | 
| `test` | 

The following SQL query removes every `>` and `<` character from both ends of a string.

```sql
SELECT trim('>>>>test<<', '><') AS result
```

The query returns the following result.


| result | 
| --- | 
| `test` | 

The following SQL query uses the SQL standard form to remove `x` characters from the start of a string only.

```sql
SELECT trim(LEADING 'x' FROM 'xxabcxx') AS result
```

The query returns the following result.


| result | 
| --- | 
| `abcxx` | 

The following SQL query trims a string that starts and ends with a tab character, `chr(9)`. Without `characters`, the tabs stay. Listing the space, tab, line feed, and carriage return characters in `characters` removes them.

```sql
SELECT
    trim(
        chr(9) || 'test' || chr(9)
    ) = 'test' AS default_trim,
    trim(
        chr(9) || 'test' || chr(9),
        ' ' || chr(9) || chr(10) || chr(13)
    ) = 'test' AS whitespace_trim
```

The query returns the following result.


| default\_trim | whitespace\_trim | 
| --- | --- | 
| `false` | `true` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-trim-differences-from-engine-version-3"></a>

In Athena engine version 3, `trim` without `characters` also removes leading and trailing tabs and line breaks. In DuckDB, it removes only space characters. To also remove tabs and line breaks, list them in `characters`, for example `trim({{string}}, ' ' || chr(9) || chr(10) || chr(13))`.

## `upper` function
<a name="athena-duckdb-functions-string-upper"></a>

Returns `string` with each lowercase letter converted to uppercase. The conversion follows Unicode case rules, so letters outside ASCII are converted too. Characters that are not letters are unchanged. If `string` is `NULL`, the result is `NULL`.

`ucase` is an alias of `upper`. To convert a string to lowercase, use the [`lower` function](#athena-duckdb-functions-string-lower).

### Syntax
<a name="athena-duckdb-functions-string-upper-syntax"></a>

```sql
upper({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-upper-arguments"></a>

The `upper` function takes the following argument:

**{{string}}**  
The string to convert, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-upper-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-upper-examples"></a>

The following SQL query converts a string to uppercase.

```sql
SELECT upper('Hello') AS result
```

The query returns the following result.


| result | 
| --- | 
| `HELLO` | 

The following SQL query uses the `ucase` alias. The digits and the space are unchanged.

```sql
SELECT ucase('abc 123') AS result
```

The query returns the following result.


| result | 
| --- | 
| `ABC 123` | 

The following SQL query converts the German sharp s, `chr(223)`, and returns the code point of the result. The result is `7838`, the code point of the capital sharp s.

```sql
SELECT unicode(upper(chr(223))) AS result
```

The query returns the following result.


| result | 
| --- | 
| `7838` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-upper-differences-from-engine-version-3"></a>

In Athena engine version 3, `upper` leaves the German sharp s (code point 223) unchanged. DuckDB converts it to the capital sharp s (code point 7838).