View a markdown version of this page

Collations and sort keys - Amazon Athena

Collations and sort keys

Use collations to compare and sort strings while ignoring case or accents, or to follow the rules of a language.

A sort key turns a string into a value that sorts in the same order as the string does under a language's collation.

Collations

A collation sets the rules that DuckDB uses to compare strings. It changes the results of comparisons such as =, <, and IN, and the order of the rows that ORDER BY returns. It does not change the string values themselves.

Without a collation, strings compare by their bytes. All uppercase ASCII letters sort before all lowercase letters, so Bob sorts before adam, and an accented letter does not equal the same letter without the accent.

To apply a collation, write COLLATE and the collation name after a string expression, for example ORDER BY name COLLATE NOCASE. Collation names are not case-sensitive. If you name a collation that does not exist, the query fails.

Use NOCASE to ignore the difference between uppercase and lowercase letters, and NOACCENT to ignore accent marks. To combine collations, join their names with a period, for example NOCASE.NOACCENT.

To compare and sort by the rules of a language, use a locale collation, for example COLLATE de for German or COLLATE en_us for English (United States).

The following table lists the locale names that you can use as collations.

Locale name Locale name Locale name Locale name

aa

af

am

ar

ar_sa

as

az

bal

be

bg

blo

bn

bo

br

bs

ca

ceb

chr

cs

cu

cy

da

de

de_at

dsb

dz

ee

el

en

en_us

eo

es

et

fa

fa_af

ff

fi

fil

fo

fr

fr_ca

fy

ga

gl

gu

ha

haw

he

he_il

hi

hr

hsb

hu

hy

id

id_id

ig

is

it

ja

ka

kk

kl

km

kn

ko

kok

ku

ky

lb

lij

lkt

ln

lo

lt

lv

mk

ml

mn

mr

ms

mt

my

nb

nb_no

ne

nl

nn

no

noaccent

nso

om

or

pa

pa_in

pl

ps

pt

ro

ru

sa

se

sgs

si

sk

sl

smn

sq

sr

sr_ba

sr_me

sr_rs

ssy

st

sv

sw

ta

te

th

tk

tn

to

tr

ug

uk

ur

uz

vi

vo

wae

wo

xh

yi

yo

zh

zh_cn

zh_hk

zh_mo

zh_sg

zh_tw

zu

Syntax

expression COLLATE collation

Examples

The following SQL query compares a and B. By default, uppercase letters sort before lowercase letters, so 'a' < 'B' is false. With NOCASE, the comparison ignores case and returns true.

SELECT 'a' < 'B' AS default_less, 'a' COLLATE NOCASE < 'B' AS nocase_less

The query returns the following result.

default_less nocase_less

false

true

icu_sort_key function

Returns a sort key for string under the collation rules of locale, as a VARCHAR value of hexadecimal digits. When you compare two sort keys that use the same locale, or sort by them, you get the same order as when you compare the strings with that locale's collation. For example, in English order, apple comes before Cherry, although every uppercase ASCII letter sorts before every lowercase letter when strings are compared by their bytes.

If either argument is NULL, the result is NULL. To compare or sort strings by a locale's rules directly, without a separate key, use COLLATE, as in ORDER BY name COLLATE en. For more information about collations, see Collations.

Syntax

icu_sort_key(string, locale)

Arguments

The icu_sort_key function takes the following arguments:

string

The string to build a sort key for, as a VARCHAR value.

locale

The name of the locale whose rules to use, as a VARCHAR value such as 'en' for English or 'sv' for Swedish. Locale names are not case-sensitive.

Return type

VARCHAR

Examples

The following SQL query returns the English sort key of a string.

SELECT icu_sort_key('abc', 'en') AS result

The query returns the following result.

result

2B2D2F01070107

The following SQL query compares a with B. By the English sort keys, a comes first. By their bytes, B comes first, so the plain comparison returns false.

SELECT icu_sort_key('a', 'en') < icu_sort_key('B', 'en') AS sort_key_less, 'a' < 'B' AS byte_less

The query returns the following result.

sort_key_less byte_less

true

false

The following SQL query sorts three names by their English sort keys, so the name that starts with an uppercase letter is not sorted first.

SELECT v.name AS name FROM (VALUES ('banana'), ('Cherry'), ('apple') ) AS v(name) ORDER BY icu_sort_key(v.name, 'en')

The query returns the following results.

name

apple

banana

Cherry

The following SQL query shows that the order depends on the locale. chr(228) is the lowercase letter a with a diaeresis. Swedish sorts it after z, and German sorts it before z.

SELECT icu_sort_key( chr(228), 'sv' ) > icu_sort_key('z', 'sv') AS swedish_after_z, icu_sort_key( chr(228), 'de' ) > icu_sort_key('z', 'de') AS german_after_z

The query returns the following result.

swedish_after_z german_after_z

true

false

Considerations

A locale name that the collation library does not know does not always cause an error. With many such names, such as 'zz' or 'swedish', the function uses the default collation rules instead, so 'swedish' does not give Swedish order, but 'sv' does. Use a locale code, such as 'sv', and check its spelling. Other names that are not valid locales, such as 'notalocale', make the query fail.

Compare only sort keys that you built with the same locale. Keys from different locales do not have a meaningful order.