

# 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
<a name="athena-duckdb-functions-string-ascii"></a>

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](#athena-duckdb-functions-string-chr).

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

```sql
ascii({{string}})
```

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

The `ascii` function takes the following argument:

**{{string}}**  
The string to read the first character from, as a `VARCHAR` value.

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

`INTEGER`

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

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

```sql
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.

```sql
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
<a name="athena-duckdb-functions-string-ascii-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-string-chr"></a>

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
<a name="athena-duckdb-functions-string-chr-syntax"></a>

```sql
chr({{code_point}})
```

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

The `chr` function takes the following argument:

**{{code\_point}}**  
The Unicode code point to convert, as an `INTEGER` value.

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

`VARCHAR`

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

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

```sql
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`.

```sql
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
<a name="athena-duckdb-functions-string-chr-considerations"></a>

`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
<a name="athena-duckdb-functions-string-left-grapheme"></a>

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
<a name="athena-duckdb-functions-string-left-grapheme-syntax"></a>

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

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

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
<a name="athena-duckdb-functions-string-left-grapheme-return-type"></a>

`VARCHAR`

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

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

```sql
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.

```sql
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
<a name="athena-duckdb-functions-string-length-grapheme"></a>

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
<a name="athena-duckdb-functions-string-length-grapheme-syntax"></a>

```sql
length_grapheme({{string}})
```

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

The `length_grapheme` function takes the following argument:

**{{string}}**  
The string to count grapheme clusters in, as a `VARCHAR` value.

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

`BIGINT`

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

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

```sql
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.

```sql
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.

```sql
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
<a name="athena-duckdb-functions-string-nfc-normalize"></a>

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
<a name="athena-duckdb-functions-string-nfc-normalize-syntax"></a>

```sql
nfc_normalize({{string}})
```

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

The `nfc_normalize` function takes the following argument:

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

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

`VARCHAR`

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

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.

```sql
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.

```sql
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
<a name="athena-duckdb-functions-string-nfc-normalize-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-string-right-grapheme"></a>

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
<a name="athena-duckdb-functions-string-right-grapheme-syntax"></a>

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

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

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
<a name="athena-duckdb-functions-string-right-grapheme-return-type"></a>

`VARCHAR`

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

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

```sql
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`.

```sql
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
<a name="athena-duckdb-functions-string-strip-accents"></a>

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
<a name="athena-duckdb-functions-string-strip-accents-syntax"></a>

```sql
strip_accents({{string}})
```

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

The `strip_accents` function takes the following argument:

**{{string}}**  
The string to remove accents from, as a `VARCHAR` value.

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

`VARCHAR`

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

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.

```sql
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.

```sql
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`.

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

The query returns the following result.


| is\_unchanged | 
| --- | 
| `true` | 

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

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](duckdb-functions-string-collation-sort-keys.md#athena-duckdb-functions-string-collations).

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

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](duckdb-functions-list-length-and-elements.md#athena-duckdb-functions-list-length). If `string` is `NULL`, the result is `NULL`.

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

```sql
strlen({{string}})
```

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

The `strlen` function takes the following argument:

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

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

`BIGINT`

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

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

```sql
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`.

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

The query returns the following result.


| bytes | characters | 
| --- | --- | 
| `5` | `4` | 

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

`strlen` accepts only `VARCHAR` values. To count the bytes in a `BLOB` value, use the [`octet_length` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-octet-length).

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

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
<a name="athena-duckdb-functions-string-substring-grapheme-syntax"></a>

```sql
substring_grapheme({{string}}, {{start}} [, {{length}} ])
```

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

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
<a name="athena-duckdb-functions-string-substring-grapheme-return-type"></a>

`VARCHAR`

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

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

```sql
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`.

```sql
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
<a name="athena-duckdb-functions-string-unicode"></a>

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](#athena-duckdb-functions-string-chr).

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

```sql
unicode({{string}})
```

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

The `unicode` function takes the following argument:

**{{string}}**  
The string to read the first character from, as a `VARCHAR` value.

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

`INTEGER`

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

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

```sql
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`.

```sql
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`.

```sql
SELECT unicode('') AS result
```

The query returns the following result.


| result | 
| --- | 
| `-1` | 

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

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