View a markdown version of this page

General string functions - Amazon Athena

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. To test whether a string starts with, ends with, or contains another string, see Pattern matching functions and operators.

|| (concatenation) operator

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.

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

expression1 || expression2

Arguments

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

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

The following SQL query concatenates two strings.

SELECT 'Duck' || 'DB' AS result

The query returns the following result.

result

DuckDB

The following SQL query concatenates two lists.

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.

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.

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

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

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

concat(value [, ...])

Arguments

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

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

Examples

The following SQL query joins three strings.

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.

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.

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.

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

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

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

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

concat_ws(separator, value [, ...])

Arguments

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

VARCHAR

Examples

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

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.

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.

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

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

instr(string, search_string) strpos(string, search_string) position(search_string IN string)

Arguments

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

BIGINT

Examples

The following SQL query finds the first occurrence of es.

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.

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.

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.

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

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

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.

Syntax

left(string, count)

Arguments

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

VARCHAR

Examples

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

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.

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.

SELECT left('Hello', 10) AS result

The query returns the following result.

result

Hello

Considerations

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

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

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.

Syntax

lower(string)

Arguments

The lower function takes the following argument:

string

The string to convert, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query converts a string to lowercase.

SELECT lower('Hello') AS result

The query returns the following result.

result

hello

The following SQL query uses the lcase alias.

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.

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

The query returns the following result.

is_converted

true

lpad function

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

lpad(string, count, character)

Arguments

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

VARCHAR

Examples

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

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.

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.

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

The query returns the following result.

result

hel

Considerations

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

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. To remove them from both ends, use the trim function.

Syntax

ltrim(string [, characters ])

Arguments

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

VARCHAR

Examples

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

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.

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.

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.

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

The query returns the following result.

result

abc

Considerations

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

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

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

overlay(string PLACING replacement FROM start [ FOR count ]) overlay(string, replacement, start [, count ])

Arguments

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

VARCHAR

Examples

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

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.

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.

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.

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

The query returns the following result.

result

abcZ

replace function

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

replace(string, source, target)

Arguments

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

VARCHAR

Examples

The following SQL query replaces each l with a hyphen.

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.

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.

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

The query returns the following result.

result

abc

Differences from Athena engine version 3

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

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

reverse(string)

Arguments

The reverse function takes the following argument:

string

The string to reverse, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query reverses a string.

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.

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

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.

Syntax

right(string, count)

Arguments

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

VARCHAR

Examples

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

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.

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

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

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

rpad(string, count, character)

Arguments

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

VARCHAR

Examples

The following SQL query pads a string to 10 characters.

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.

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

The query returns the following result.

result

hel

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

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

The query returns the following result.

result

abcxyxy

Considerations

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

rtrim function

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

rtrim(string [, characters ])

Arguments

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

VARCHAR

Examples

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

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.

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.

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

The query returns the following result.

result

5

Considerations

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

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

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

split_part(string, delimiter, position)

Arguments

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

VARCHAR

Examples

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

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.

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.

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

The query returns the following result.

result

c

Differences from Athena engine version 3

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

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.

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

string_split(string, separator)

Arguments

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

VARCHAR[]

Examples

The following SQL query splits a string at each hyphen.

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.

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.

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

The query returns the following result.

part_count

1

Differences from Athena engine version 3

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

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

substring(string, start [, length ]) substring(string FROM start [ FOR length ])

Arguments

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

VARCHAR

Examples

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

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.

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.

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.

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

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

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. If any argument is NULL, the result is NULL.

Syntax

translate(string, from, to)

Arguments

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

VARCHAR

Examples

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

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.

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

The query returns the following result.

result

20261118

Considerations

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

trim function

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 or the rtrim function. You can also use the SQL standard form with LEADING or TRAILING.

Syntax

trim(string [, characters ]) trim([ BOTH | LEADING | TRAILING ] [ characters ] FROM string)

Arguments

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

VARCHAR

Examples

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

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.

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.

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.

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

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

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.

Syntax

upper(string)

Arguments

The upper function takes the following argument:

string

The string to convert, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query converts a string to uppercase.

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.

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.

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

The query returns the following result.

result

7838

Differences from Athena engine version 3

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).