View a markdown version of this page

Pattern matching functions and operators - Amazon Athena

Pattern matching functions and operators

Use these operators and functions to test whether a string matches a LIKE or GLOB pattern, or whether it starts with, ends with, or contains another string.

The tests are case-sensitive, except ILIKE, ilike_escape, and not_ilike_escape, which ignore case.

^@ (starts with) operator

Returns true if search_string is a prefix of string, and false if it is not. The comparison is case-sensitive, and % and _ are ordinary characters, not wildcards. An empty search_string matches every string. If either argument is NULL, returns NULL.

starts_with is an alias of ^@. Use it as a function: starts_with(string, search_string) returns the same result as string ^@ search_string.

Syntax

string ^@ search_string starts_with(string, search_string)

Arguments

The ^@ (starts with) operator takes the following arguments:

string

The string to test, as a VARCHAR value.

search_string

The prefix to look for, as a VARCHAR value.

Return type

BOOLEAN

Examples

The following SQL query tests a prefix with the operator and with the starts_with function.

SELECT 'abc' ^@ 'ab' AS operator_form, starts_with('abc', 'a') AS function_form

The query returns the following result.

operator_form function_form

true

true

The following SQL query shows that the match is case-sensitive, that an empty prefix always matches, that % is not a wildcard, and that a NULL input returns NULL.

SELECT starts_with('abc', 'A') AS upper_prefix, starts_with('abc', '') AS empty_prefix, starts_with('abc', 'a%') AS percent_prefix, starts_with(NULL, 'a') AS null_input

The query returns the following result.

upper_prefix empty_prefix percent_prefix null_input

false

true

false

NULL

Differences from Athena engine version 3

Athena engine version 3 does not support the ^@ operator. For a query that runs on both engines, use the starts_with function.

contains function

With a string, returns true if search_value appears anywhere in value. The search is case-sensitive and has no wildcards, and an empty search_value always matches.

With a list, returns true if search_value is an element of the list. A list that holds NULL but not search_value returns false, not NULL. With a MAP, returns true if search_value is one of the map's keys. With an unnamed struct, such as one built with row, returns true if one of its fields equals search_value.

If value or search_value is NULL, returns NULL, not false.

Syntax

contains(value, search_value)

Arguments

The contains function takes the following arguments:

value

The value to search: a VARCHAR string, a list, a MAP, or an unnamed STRUCT.

search_value

What to look for: a VARCHAR substring for a string, an element for a list, a key for a map, or a field value for an unnamed struct.

Return type

BOOLEAN

Examples

The following SQL query tests whether a string contains a letter.

SELECT contains('abc', 'a') AS result

The query returns the following result.

result

true

The following SQL query shows that the search is case-sensitive, that an empty search string matches, and that % is not a wildcard.

SELECT contains('abc', 'A') AS upper_case, contains('abc', '') AS empty_search, contains('abc', 'a%') AS percent_search

The query returns the following result.

upper_case empty_search percent_search

false

true

false

The following SQL query searches a list, a list that holds NULL, the keys of a map, and an unnamed struct.

SELECT contains([1, 2, 3], 2) AS in_list, contains([1, NULL], 3) AS list_with_null, contains(MAP {'a': 1, 'b': 2}, 'b') AS map_key, contains(row(1, 'x'), 'x') AS struct_field

The query returns the following result.

in_list list_with_null map_key struct_field

true

false

true

true

The following SQL query casts the NULL literal to VARCHAR, so the string form runs and returns NULL. Without the cast, the query fails.

SELECT contains('abc', NULL::VARCHAR) AS result

The query returns the following result.

result

NULL

The following SQL query passes an untyped NULL literal as search_value to the list, map, and struct forms. Each form returns NULL.

SELECT contains([1, 2, 3], NULL) AS list_form, contains(MAP {'a': 1}, NULL) AS map_form, contains(row(1, 'x'), NULL) AS struct_form

The query returns the following result.

list_form map_form struct_form

NULL

NULL

NULL

Considerations

With a string, an untyped NULL literal as search_value, as in contains('abc', NULL), makes the query fail, because more than one form of the function matches. Cast the literal, for example NULL::VARCHAR. With a list, a map, or an unnamed struct, an untyped NULL literal works, and the result is NULL.

The struct form works only with unnamed structs. A struct with named fields causes an error.

Differences from Athena engine version 3

In Athena engine version 3, contains on an array returns NULL when the array holds a NULL element and no match. In DuckDB, it returns false.

In Athena engine version 3, contains takes an array, not a string. To test for a substring in a query that runs on both engines, use strpos(value, search_value) > 0.

GLOB operator

Returns true if string matches pattern, and false if it does not. The pattern uses the wildcards of file-name matching and must match the whole string. In the pattern, * matches any sequence of zero or more characters, ? matches exactly one character, and [abc] or [a-c] matches one character from a set or range. A negated set, such as [!abc], matches one character that is not in the set. Matching is case-sensitive.

NOT GLOB returns the opposite result. If string or pattern is NULL, both forms return NULL. To match a literal *, ?, or [, put it in brackets, such as [*].

You can also write GLOB as the ~~~ operator.

Syntax

string [ NOT ] GLOB pattern string ~~~ pattern

Arguments

The GLOB operator takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The pattern to match, as a VARCHAR value written with *, ?, and bracket wildcards.

Return type

BOOLEAN

Examples

The following SQL query tests file names against patterns that use * and ?.

SELECT 'data.csv' GLOB '*.csv' AS csv_file, 'data.CSV' GLOB '*.csv' AS upper_case_name, 'file1.csv' GLOB 'file?.csv' AS one_char

The query returns the following result.

csv_file upper_case_name one_char

true

false

true

The following SQL query uses a character range, a negated set, NOT GLOB, and a NULL input.

SELECT 'b1' GLOB '[a-c][0-9]' AS in_range, 'd1' GLOB '[!a-c]?' AS not_in_set, 'data.csv' NOT GLOB '*.json' AS not_glob, NULL GLOB '*' AS null_input

The query returns the following result.

in_range not_in_set not_glob null_input

true

true

true

NULL

The following SQL query matches a literal asterisk by putting it in brackets, and uses the ~~~ operator form.

SELECT 'a*b' GLOB 'a[*]b' AS literal_star, 'axb' GLOB 'a[*]b' AS other_char, 'abc' ~~~ 'a*' AS glob_operator

The query returns the following result.

literal_star other_char glob_operator

true

false

true

Considerations

GLOB does not take an ESCAPE clause. Instead, a backslash in the pattern makes the next character match literally, so 'a*b' GLOB 'a\*b' returns true. A backslash in the pattern does not match a backslash in the string, so 'a\b' GLOB 'a\b' returns false. To match a literal backslash, put it in brackets, such as [\].

To negate a set, use [!abc]. In [^abc], the ^ is an ordinary member of the set, not a negation.

To match regardless of case, convert the string to lowercase and write the pattern in lowercase, for example lower(file_name) GLOB '*.csv'.

Differences from Athena engine version 3

Athena engine version 3 does not support GLOB. For a pattern match that runs on both engines, use LIKE, with % in place of * and _ in place of ?. In LIKE, % and _ are wildcards, so a % or _ that the GLOB pattern matches literally needs an ESCAPE character. LIKE has no bracket sets such as [a-c].

ILIKE operator

Returns true if string matches pattern when the case of letters is ignored, and false if it does not. The pattern must match the whole string, and it uses the same wildcards as LIKE: % matches any sequence of zero or more characters, and _ matches exactly one character.

NOT ILIKE returns the opposite result. If string or pattern is NULL, both forms return NULL. To match a literal % or _, name an escape character with ESCAPE.

You can also write ILIKE as the ~~* operator and NOT ILIKE as the !~~* operator.

Syntax

string [ NOT ] ILIKE pattern [ ESCAPE escape_character ] string ~~* pattern string !~~* pattern

Arguments

The ILIKE operator takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The pattern to match, as a VARCHAR value. Use % for any sequence of characters and _ for one character.

escape_character

The escape character, a VARCHAR value of one character. In the pattern, this character makes the next %, _, or escape character match literally. An empty string means that there is no escape character. This argument is optional.

Return type

BOOLEAN

Examples

The following SQL query compares ILIKE with LIKE on an uppercase string.

SELECT 'ABC' ILIKE 'a%' AS ilike_result, 'ABC' ILIKE 'a_c' AS three_chars, 'ABC' LIKE 'a%' AS like_result

The query returns the following result.

ilike_result three_chars like_result

true

true

false

The following SQL query uses NOT ILIKE, an escaped percent sign, and a NULL input.

SELECT 'ABC' NOT ILIKE 'b%' AS not_ilike, '10% OFF' ILIKE '10!% off' ESCAPE '!' AS escaped_percent, NULL ILIKE 'a%' AS null_input

The query returns the following result.

not_ilike escaped_percent null_input

true

true

NULL

The following SQL query uses the operator forms ~~* and !~~*.

SELECT 'ABC' ~~* 'a%' AS ilike_operator, 'ABC' !~~* 'a%' AS not_ilike_operator

The query returns the following result.

ilike_operator not_ilike_operator

true

false

Differences from Athena engine version 3

Athena engine version 3 does not support ILIKE. For a match that ignores case and runs on both engines, use lower(string) LIKE lower(pattern).

ilike_escape function

Tests whether string matches the LIKE pattern in pattern, ignoring case. The result is true for a match and false when there is no match. In the pattern, % matches any sequence of characters and _ matches any single character. Put escape_character in front of %, _, or the escape character itself to match that character literally. If any argument is NULL, the result is NULL.

ilike_escape(string, pattern, escape_character) returns the same result as string ILIKE pattern ESCAPE escape_character. For a case-sensitive match, use the like_escape function.

Syntax

ilike_escape(string, pattern, escape_character)

Arguments

The ilike_escape function takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The LIKE pattern, as a VARCHAR value.

escape_character

The escape character, a VARCHAR value of one ASCII character. An empty string means that the pattern has no escape character.

Return type

BOOLEAN

Examples

The following SQL query matches a literal percent sign, ignoring case. The pattern uses $ as the escape character.

SELECT ilike_escape('A%c', 'a$%C', '$') AS result

The query returns the following result.

result

true

The following SQL query returns false, because the escaped % matches only a literal percent sign, not the letter b.

SELECT ilike_escape('Abc', 'a$%C', '$') AS result

The query returns the following result.

result

false

The following SQL query writes the same test with the ILIKE operator and an ESCAPE clause.

SELECT 'A%c' ILIKE 'a$%C' ESCAPE '$' AS result

The query returns the following result.

result

true

Considerations

If escape_character is longer than one byte, the query fails. A non-ASCII character, such as chr(228), is longer than one byte, so it cannot be the escape character.

If pattern ends with an escape_character that has no character after it to escape, the query fails.

LIKE operator

Returns true if string matches pattern, and false if it does not. The pattern must match the whole string. In the pattern, % matches any sequence of zero or more characters, and _ matches exactly one character. Every other character matches only itself, and letters must match in case.

NOT LIKE returns the opposite result. If string or pattern is NULL, both forms return NULL. To match a literal % or _, name an escape character with ESCAPE and put it before the wildcard in the pattern.

You can also write LIKE as the ~~ operator and NOT LIKE as the !~~ operator. For a match that ignores case, use ILIKE.

Syntax

string [ NOT ] LIKE pattern [ ESCAPE escape_character ] string ~~ pattern string !~~ pattern

Arguments

The LIKE operator takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The pattern to match, as a VARCHAR value. Use % for any sequence of characters and _ for one character.

escape_character

The escape character, a VARCHAR value of one character. In the pattern, this character makes the next %, _, or escape character match literally. An empty string means that there is no escape character. This argument is optional.

Return type

BOOLEAN

Examples

The following SQL query tests one string against a prefix pattern, a pattern of exactly three characters, and a pattern with no wildcards.

SELECT 'abc' LIKE 'a%' AS starts_with_a, 'abc' LIKE '_b_' AS three_chars, 'abc' LIKE 'b' AS exact_b

The query returns the following result.

starts_with_a three_chars exact_b

true

true

false

The following SQL query shows that LIKE and = are both case-sensitive, that NOT LIKE returns the opposite result, and that a NULL input returns NULL.

SELECT 'abc' LIKE 'A%' AS upper_pattern, 'abc' NOT LIKE 'A%' AS not_like, 'abc' = 'ABC' AS equals_upper, NULL LIKE 'a%' AS null_input

The query returns the following result.

upper_pattern not_like equals_upper null_input

false

true

false

NULL

The following SQL query uses ESCAPE to match a literal percent sign and a literal underscore. Without an ESCAPE clause, the backslash is an ordinary character.

SELECT '50% off' LIKE '50!%%' ESCAPE '!' AS escaped_percent, 'a_c' LIKE 'a\_c' AS backslash_without_escape, 'a_c' LIKE 'a\_c' ESCAPE '\' AS backslash_with_escape

The query returns the following result.

escaped_percent backslash_without_escape backslash_with_escape

true

false

true

The following SQL query uses the operator forms ~~ and !~~.

SELECT 'abc' ~~ 'a%' AS like_operator, 'abc' !~~ 'a%' AS not_like_operator

The query returns the following result.

like_operator not_like_operator

true

false

Considerations

LIKE has no default escape character. A backslash in the pattern matches a literal backslash unless you name it with ESCAPE '\'.

The ESCAPE value must be one character or an empty string. A longer value causes an error.

To test for a fixed prefix or substring without escaping wildcards, use the ^@ operator or the contains function.

like_escape function

Tests whether string matches the LIKE pattern in pattern. The result is true for a match and false when there is no match. The match is case-sensitive. In the pattern, % matches any sequence of characters and _ matches any single character. Put escape_character in front of %, _, or the escape character itself to match that character literally. If any argument is NULL, the result is NULL.

like_escape(string, pattern, escape_character) returns the same result as string LIKE pattern ESCAPE escape_character. To ignore case, use the ilike_escape function.

Syntax

like_escape(string, pattern, escape_character)

Arguments

The like_escape function takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The LIKE pattern, as a VARCHAR value.

escape_character

The escape character, a VARCHAR value of one ASCII character. An empty string means that the pattern has no escape character.

Return type

BOOLEAN

Examples

The following SQL query matches a literal percent sign. The pattern uses $ as the escape character.

SELECT like_escape('a%c', 'a$%c', '$') AS result

The query returns the following result.

result

true

The following SQL query tests whether strings start with 100%, using # as the escape character. The unescaped % at the end of the pattern matches the rest of the string.

SELECT like_escape( '100% done', '100#%%', '#' ) AS percent_value, like_escape( '1000 done', '100#%%', '#' ) AS other_value

The query returns the following result.

percent_value other_value

true

false

The following SQL query shows that the match is case-sensitive.

SELECT like_escape('A%c', 'a$%c', '$') AS result

The query returns the following result.

result

false

Considerations

If escape_character is longer than one byte, the query fails. A non-ASCII character, such as chr(228), is longer than one byte, so it cannot be the escape character.

If pattern ends with an escape_character that has no character after it to escape, the query fails.

not_ilike_escape function

Returns true if string does not match pattern when case is ignored, and false if it does. In the pattern, % matches any sequence of characters and _ matches any single character. Put escape_character in front of %, _, or the escape character itself to match that character literally. If any argument is NULL, the result is NULL.

not_ilike_escape(string, pattern, escape_character) returns the same result as string NOT ILIKE pattern ESCAPE escape_character. For a case-sensitive test, use the not_like_escape function.

Syntax

not_ilike_escape(string, pattern, escape_character) string NOT ILIKE pattern ESCAPE escape_character

Arguments

The not_ilike_escape function takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The LIKE pattern, as a VARCHAR value.

escape_character

The escape character, a VARCHAR value of one character. An empty string means that the pattern has no escape character.

Return type

BOOLEAN

Examples

The following SQL query returns false because the string matches the pattern when case is ignored. The pattern uses $ as the escape character, so $% matches only a literal percent sign.

SELECT not_ilike_escape('A%c', 'a$%C', '$') AS result

The query returns the following result.

result

false

The following SQL query returns true because the string has no percent sign for the escaped % to match.

SELECT not_ilike_escape('Abc', 'a$%c', '$') AS result

The query returns the following result.

result

true

The following SQL query runs the test from the first example with the NOT ILIKE operator and an ESCAPE clause.

SELECT 'A%c' NOT ILIKE 'a$%C' ESCAPE '$' AS result

The query returns the following result.

result

false

Considerations

If escape_character is longer than one character, the query fails.

not_like_escape function

Returns true if string does not match pattern, and false if it does. The match is case-sensitive. In the pattern, % matches any sequence of characters and _ matches any single character. Put escape_character in front of %, _, or the escape character itself to match that character literally. If any argument is NULL, the result is NULL.

not_like_escape(string, pattern, escape_character) returns the same result as string NOT LIKE pattern ESCAPE escape_character. To ignore case, use the not_ilike_escape function.

Syntax

not_like_escape(string, pattern, escape_character) string NOT LIKE pattern ESCAPE escape_character

Arguments

The not_like_escape function takes the following arguments:

string

The string to test, as a VARCHAR value.

pattern

The LIKE pattern, as a VARCHAR value.

escape_character

The escape character, a VARCHAR value of one character. An empty string means that the pattern has no escape character.

Return type

BOOLEAN

Examples

The following SQL query returns false because the string matches the pattern. The pattern uses $ as the escape character, so $% matches only a literal percent sign.

SELECT not_like_escape('a%c', 'a$%c', '$') AS result

The query returns the following result.

result

false

The following SQL query returns true because the match is case-sensitive, and A does not match a.

SELECT not_like_escape('A%c', 'a$%c', '$') AS result

The query returns the following result.

result

true

The following SQL query runs the test from the first example with the NOT LIKE operator and an ESCAPE clause.

SELECT 'a%c' NOT LIKE 'a$%c' ESCAPE '$' AS result

The query returns the following result.

result

false

Considerations

If escape_character is longer than one character, the query fails.

prefix function

Returns true if string begins with search_string, and false otherwise. The comparison is case-sensitive, and an empty search_string matches every string. If either argument is NULL, the result is NULL.

The starts_with function and the ^@ operator run the same test. To test the end of a string, use the suffix function.

Syntax

prefix(string, search_string)

Arguments

The prefix function takes the following arguments:

string

The string to test, as a VARCHAR value.

search_string

The string to look for at the start of string, as a VARCHAR value.

Return type

BOOLEAN

Examples

The following SQL query tests whether a string starts with ab.

SELECT prefix('abc', 'ab') AS result

The query returns the following result.

result

true

The following SQL query returns false because the comparison is case-sensitive.

SELECT prefix('abc', 'AB') AS result

The query returns the following result.

result

false

The following SQL query returns true because an empty search string matches every string.

SELECT prefix('abc', '') AS result

The query returns the following result.

result

true

suffix function

Checks whether the last characters of string are the same as search_string, and returns true or false. The comparison is case-sensitive. An empty search_string matches every string. If either argument is NULL, the result is NULL.

ends_with is an alias of suffix. To test the start of a string, use the prefix function or starts_with.

Syntax

suffix(string, search_string)

Arguments

The suffix function takes the following arguments:

string

The string to test, as a VARCHAR value.

search_string

The string to look for at the end of string, as a VARCHAR value.

Return type

BOOLEAN

Examples

The following SQL query tests whether a string ends with bc.

SELECT suffix('abc', 'bc') AS result

The query returns the following result.

result

true

The following SQL query uses the ends_with alias. The comparison is case-sensitive, so BC does not match.

SELECT ends_with('abc', 'BC') AS result

The query returns the following result.

result

false