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 . Collation names are not case-sensitive. If you name a collation that does not exist, the query fails.name COLLATE NOCASE
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 |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|---|---|
|
|
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 . For more information about collations, see Collations.name COLLATE en
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
VARCHARvalue. locale-
The name of the locale whose rules to use, as a
VARCHARvalue 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 |
|---|
|
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 |
|---|---|
|
|
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 |
|---|
|
|
|
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 |
|---|---|
|
|
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.