View a markdown version of this page

Unicode character functions - Amazon Athena

Unicode character functions

Use these functions to convert between characters and Unicode code points, normalize text, remove accent marks, and count the bytes in a string.

Functions whose names end in _grapheme count and take characters as a reader sees them, such as a letter followed by a combining accent.

ascii function

Converts the first character of string to its Unicode code point and returns that number as an INTEGER. Characters after the first are ignored. Despite the name, the result is not limited to ASCII: for a non-ASCII character, it is the character's Unicode code point.

For an empty string, returns 0. For NULL, returns NULL. To convert a code point back to a character, use the chr function.

Syntax

ascii(string)

Arguments

The ascii function takes the following argument:

string

The string to read the first character from, as a VARCHAR value.

Return type

INTEGER

Examples

The following SQL query returns the code point of the letter A.

SELECT ascii('A') AS result

The query returns the following result.

result

65

The following SQL query shows that only the first character counts, that the non-ASCII character chr(937) returns its code point 937, and how empty and NULL inputs behave.

SELECT ascii('Apple') AS first_char_only, ascii(chr(937)) AS non_ascii, ascii('') AS empty_string, ascii(NULL) AS null_input

The query returns the following result.

first_char_only non_ascii empty_string null_input

65

937

0

NULL

Differences from Athena engine version 3

Athena engine version 3 does not have an ascii function.

In Athena engine version 3, the closest function is codepoint, which accepts only a string of exactly one character. To get the code point of the first character of a longer string in Athena engine version 3, use codepoint(CAST(string AS VARCHAR(1))).

chr function

Returns the character whose Unicode code point is code_point, as a one-character VARCHAR. For values from 0 to 127, this is the ASCII character with that code. chr is the inverse of ascii.

Use chr to put characters that are hard to type into a string, such as chr(10) for a line feed. If code_point is NULL, returns NULL. If it is not a valid Unicode code point, such as a negative number or a number greater than 1114111, the query fails.

Syntax

chr(code_point)

Arguments

The chr function takes the following argument:

code_point

The Unicode code point to convert, as an INTEGER value.

Return type

VARCHAR

Examples

The following SQL query returns the character for code point 65.

SELECT chr(65) AS result

The query returns the following result.

result

A

The following SQL query builds a string from two code points, returns the length and code point of a non-ASCII character, and passes NULL.

SELECT chr(97) || chr(98) AS two_chars, length(chr(937)) AS non_ascii_length, ascii(chr(937)) AS round_trip, chr(NULL) AS null_input

The query returns the following result.

two_chars non_ascii_length round_trip null_input

ab

1

937

NULL

Considerations

chr takes an INTEGER. A BIGINT, DOUBLE, or DECIMAL argument causes an error, so cast it first, for example chr(CAST(x AS INTEGER)).

left_grapheme function

Returns the first count grapheme clusters of string. A grapheme cluster is what a reader sees as one character, even when it is made of several Unicode code points. For example, a letter followed by a combining accent is one grapheme cluster, and so is an emoji with a skin tone modifier.

For text in which every character is one code point, such as plain ASCII text, left_grapheme returns the same result as left. As with left, a negative count drops that many grapheme clusters from the end. If either argument is NULL, the result is NULL.

Syntax

left_grapheme(string, count)

Arguments

The left_grapheme function takes the following arguments:

string

The string to take grapheme clusters from, as a VARCHAR value.

count

The number of grapheme clusters to return, as a BIGINT value. A negative value drops that many grapheme clusters from the end instead.

Return type

VARCHAR

Examples

The following SQL query returns the first two grapheme clusters of an ASCII string.

SELECT left_grapheme('Hello', 2) AS result

The query returns the following result.

result

He

The following SQL query builds a string from the letter e, a combining acute accent (code point 769), and the letter x. For a count of 1, left returns only the e, and left_grapheme returns the e with its accent, which is two code points.

SELECT length( left('e' || chr(769) || 'x', 1) ) AS left_length, length( left_grapheme('e' || chr(769) || 'x', 1) ) AS left_grapheme_length

The query returns the following result.

left_length left_grapheme_length

1

2

length_grapheme function

Counts the grapheme clusters in string. A grapheme cluster is what a reader sees as one character, even when it is made of several Unicode code points. The length function counts code points instead, so the two functions return different results for text with combining marks or emoji that are built from several code points.

For an empty string, the result is 0. If string is NULL, the result is NULL.

Syntax

length_grapheme(string)

Arguments

The length_grapheme function takes the following argument:

string

The string to count grapheme clusters in, as a VARCHAR value.

Return type

BIGINT

Examples

The following SQL query counts the grapheme clusters in an ASCII string.

SELECT length_grapheme('abc') AS result

The query returns the following result.

result

3

The following SQL query builds the letter e followed by a combining acute accent (code point 769). length counts two code points, and length_grapheme counts one grapheme cluster.

SELECT length('e' || chr(769)) AS code_points, length_grapheme( 'e' || chr(769) ) AS grapheme_clusters

The query returns the following result.

code_points grapheme_clusters

2

1

The following SQL query builds a face palm emoji with a skin tone and a gender sign from five code points. The length function counts five code points, and length_grapheme counts one grapheme cluster.

SELECT length( chr(129318) || chr(127996) || chr(8205) || chr(9794) || chr(65039) ) AS code_points, length_grapheme( chr(129318) || chr(127996) || chr(8205) || chr(9794) || chr(65039) ) AS grapheme_clusters

The query returns the following result.

code_points grapheme_clusters

5

1

nfc_normalize function

Returns string in Unicode Normalization Form C (NFC). In NFC, a letter followed by a combining mark, such as an accent, is replaced by the single precomposed character for that pair when one exists. Text that is already in NFC, such as plain ASCII text, is returned unchanged. If string is NULL, the result is NULL.

Two strings can look the same but compare as unequal when one uses a precomposed character and the other uses a letter and a combining mark. Normalize both sides before you compare, group, join, or sort text that can contain both forms.

Syntax

nfc_normalize(string)

Arguments

The nfc_normalize function takes the following argument:

string

The string to normalize, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query builds a string that has the letter e followed by a combining grave accent, chr(768). Normalization combines the two code points into one character, so the length drops from 7 to 6.

SELECT length('arde' || chr(768) || 'ch') AS before_length, length( nfc_normalize('arde' || chr(768) || 'ch') ) AS after_length

The query returns the following result.

before_length after_length

7

6

The following SQL query compares the letter e plus a combining acute accent, chr(769), with the precomposed character chr(233). The two are equal only after normalization.

SELECT ('e' || chr(769)) = chr(233) AS raw_equal, nfc_normalize( 'e' || chr(769) ) = chr(233) AS normalized_equal

The query returns the following result.

raw_equal normalized_equal

false

true

Differences from Athena engine version 3

Athena engine version 3 names this function normalize. DuckDB has no normalize function, so replace normalize(string) with nfc_normalize(string). nfc_normalize takes no form argument and always returns NFC.

right_grapheme function

Returns the last count grapheme clusters of string. A grapheme cluster is what a reader sees as one character, such as a letter followed by a combining accent, even when it is made of several Unicode code points.

Use right_grapheme instead of right when the end of the string can contain combining marks. For plain ASCII text, both functions return the same result.

Syntax

right_grapheme(string, count)

Arguments

The right_grapheme function takes the following arguments:

string

The string to take grapheme clusters from, as a VARCHAR value.

count

The number of grapheme clusters to return, as a BIGINT value.

Return type

VARCHAR

Examples

The following SQL query returns the last three grapheme clusters of an ASCII string.

SELECT right_grapheme('Hello', 3) AS result

The query returns the following result.

result

llo

The following SQL query builds cafe followed by a combining acute accent (chr(769)). It passes a count of 1 to each function and returns the length of each result. The right_grapheme function returns the e together with its accent, so its result has a length of 2. The right function returns only the accent, so its result has a length of 1.

SELECT length( right_grapheme('cafe' || chr(769), 1) ) AS grapheme_result_length, length( right('cafe' || chr(769), 1) ) AS right_result_length

The query returns the following result.

grapheme_result_length right_result_length

2

1

strip_accents function

Returns string with the accent marks removed from its letters. For example, an e with an acute accent becomes a plain e, and an A with a ring becomes a plain A. Letters that have no accent to remove, such as the German sharp s, stay unchanged.

Use strip_accents to match text without regard to accents, for example in lower(strip_accents(name)) = 'cafe'. If string is NULL, the result is NULL.

Syntax

strip_accents(string)

Arguments

The strip_accents function takes the following argument:

string

The string to remove accents from, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query builds the word cafe with an accented final letter, using chr(233) for the accented e, and then removes the accent.

SELECT strip_accents('caf' || chr(233)) AS result

The query returns the following result.

result

cafe

The following SQL query compares a capitalized, accented word with plain lowercase text. chr(201) is an uppercase E with an acute accent. Combining lower and strip_accents makes the comparison ignore both case and accents.

SELECT lower( strip_accents('Caf' || chr(201)) ) = 'cafe' AS is_match

The query returns the following result.

is_match

true

The following SQL query shows that the German sharp s, chr(223), is not an accented letter. strip_accents leaves it unchanged and does not convert it to ss.

SELECT strip_accents( 'stra' || chr(223) || 'e' ) = 'stra' || chr(223) || 'e' AS is_unchanged

The query returns the following result.

is_unchanged

true

Considerations

To compare strings without regard to accents while keeping the original values, you can also use the NOACCENT collation. For more information about collations, see Collations.

strlen function

Returns the number of bytes in the UTF-8 encoding of string. For ASCII text, this is the same as the number of characters. Each non-ASCII character takes two to four bytes, so for such text strlen returns more than the number of characters.

To count characters instead of bytes, use the length function. If string is NULL, the result is NULL.

Syntax

strlen(string)

Arguments

The strlen function takes the following argument:

string

The string to measure, as a VARCHAR value.

Return type

BIGINT

Examples

The following SQL query returns the number of bytes in an ASCII string.

SELECT strlen('abc') AS result

The query returns the following result.

result

3

The following SQL query compares strlen with length for a string that ends with an accented e, chr(233). The accented letter takes two bytes, so strlen returns 5 while length returns 4.

SELECT strlen('caf' || chr(233)) AS bytes, length('caf' || chr(233)) AS characters

The query returns the following result.

bytes characters

5

4

Considerations

strlen accepts only VARCHAR values. To count the bytes in a BLOB value, use the octet_length function.

substring_grapheme function

Returns part of string in the same way as substring, but counts start and length in grapheme clusters instead of characters. A grapheme cluster is what a reader sees as one character. For example, a letter followed by a combining accent mark is one grapheme cluster, and so is an emoji that is built from several code points.

Use substring_grapheme when the result must not split a grapheme cluster. For text in which each character is its own grapheme cluster, such as plain ASCII, the result is the same as for substring. A negative start counts back from the end of the string. If any argument is NULL, the result is NULL.

Syntax

substring_grapheme(string, start [, length ])

Arguments

The substring_grapheme function takes the following arguments:

string

The string to take grapheme clusters from, as a VARCHAR value.

start

The position of the first grapheme cluster to return, as a BIGINT value. Position 1 is the first grapheme cluster.

length

The number of grapheme clusters to return, as a BIGINT value. If you omit it, the result runs to the end of the string. This argument is optional.

Return type

VARCHAR

Examples

The following SQL query returns three grapheme clusters, starting at the second one. For ASCII text, each character is one grapheme cluster.

SELECT substring_grapheme('abcdef', 2, 3) AS result

The query returns the following result.

result

bcd

The following SQL query builds a string that starts with an e followed by a combining acute accent, chr(769). The first grapheme cluster holds both code points, so its length is 2. substring splits the cluster and returns only the e, so its length is 1.

SELECT length( substring_grapheme('e' || chr(769) || 'x', 1, 1) ) AS grapheme_part, length( substring('e' || chr(769) || 'x', 1, 1) ) AS substring_part

The query returns the following result.

grapheme_part substring_part

2

1

unicode function

Returns the Unicode code point of the first character in string, as an INTEGER. The other characters are ignored. For an empty string, returns -1. If string is NULL, the result is NULL.

ord is an alias of unicode. To convert a code point to a character, use the chr function.

Syntax

unicode(string)

Arguments

The unicode function takes the following argument:

string

The string to read the first character from, as a VARCHAR value.

Return type

INTEGER

Examples

The following SQL query returns the code point of the letter A.

SELECT unicode('A') AS result

The query returns the following result.

result

65

The following SQL query uses the ord alias. Only the first character counts, so the result is the code point of a.

SELECT ord('abc') AS result

The query returns the following result.

result

97

The following SQL query passes an empty string, which returns -1, not NULL.

SELECT unicode('') AS result

The query returns the following result.

result

-1

Differences from Athena engine version 3

Athena engine version 3 names this function codepoint. DuckDB has no codepoint function, so use unicode or ord instead.