|
|
Using Collations - Examples
The following sections contain examples of how to use collations and what effects collations can have on a result set.
colsweuses theswedish_1collation,colenguses theenglish_1collation andcol1uses the Mimer SQL defaultISO8BITcollation.ORDER BY
You can use a
COLLATEclause together with anORDER BYclause to sort result sets. For example, the following statement:SELECT * FROM table1 ORDER BY col1 COLLATE swedish_1;retrieves the data and sorts it on
col1according to theswedish_1collation:SELECT * FROM table1 ORDER BY col1 COLLATE english_1;retrieves the data and sorts it according to the
english_1collation:Note: Where the collation defines a number of characters with the same sort-order value, the retrieval order within the sort-order value is not defined.
GROUP BY
Depending on the collation associated with a column, you might get differing results when using
GROUP BY.SELECT col1, COUNT(*) FROM table1 GROUP BY col1 COLLATE swedish_1;According to the
swedish_1collation, two instances of the character `a' were found and one instance of `å' which is considered a separate character in the Swedish language.Similarly, using the
english_1collation in the statement:SELECT col1, COUNT(*) FROM table1 GROUP BY col1 COLLATE english_1;According to the
english_1collation, three instances of the character `a' were found, as the character `å' has the same sort-order value as `A' and `a'.Comparison Operators
SELECT col1 FROM table1 WHERE col1 > 'm';would give the following result:
However, explicitly using the
COLLATEclause and theenglish_1collation:SELECT col1 FROM table1 WHERE col1 > 'm' COLLATE english_1;would give the following result:
Similarly, explicitly using the
COLLATEclause and theswedish_1collation:SELECT col1 FROM table1 WHERE col1 > 'm' COLLATE swedish_1;would give the following result:
Scalar String Functions
You can use the
COLLATEclause with the scalar string functionsSUBSTRINGandTRIM.Character strings that are derived from a single string, for example, those returned from the
TRIMandSUBSTRINGfunctions, inherit the collation from the source string.TRIM and COLLATE
You should be aware of the consequences when you use a
TRIMstatement on a column that has a collation.For example, referring to table1, see Using Collations - Examples, the following statement:
SELECT TRIM('v' FROM colswe) FROM table1;would trim both `
W' and `v' from the result set as the characters `W' and `v' have the same sort-order value in a Swedish case-insensitive collation.SELECT TRIM('a' FROM col1)FROM table1;would trim `
A', `a' and `å' from the result set as the characters `A', `a' and `å' have the same sort-order value in an English case-insensitive collation.Concatenation Operator
Suppose you want to concatenate columns,
colsweandcol1, for example:SELECT colswe || coleng FROM table1;Because the columns use different collations the result set will have the default collation
ISO8BIT.However, if you want apply a collation to the result set, you can add a
COLLATEclause. for example:SELECT (colswe || coleng) COLLATE swedish_1 FROM table1;IN and BETWEEN
A collation will affect the results of a query that uses
INorBETWEEN.For example, the following statement:
SELECT * FROM table1 WHERE coleng BETWEEN 'a' and 'B';SELECT * FROM table1 WHERE colswe BETWEEN 'a' and 'B';UNION, EXCEPT and INTERSECT
When performing a
UNION(orEXCEPTorINTERSECT), you must know what collations are involved in order to ensure that you get the result you want.SELECT colswe FROM table1 UNION SELECT coleng FROM table1;raises an error because the
UNIONoperator can't understand which duplicate rows to remove or not.To perform the
UNIONaccording to theswedish_1collation, you would explicitly use aCOLLATEclause, for example:SELECT colswe FROM table1 UNION SELECT coleng COLLATE swedish_1 FROM table1;Similarly, for a
UNIONresult according to theenglish_1collation, you would enter:SELECT colswe COLLATE english_1 FROM table1 UNION SELECT coleng FROM table1;DISTINCT
When you use
DISTINCT, you must consider the consequences of which collation is associated with a column.SELECT DISTINCT col1 FROM table1;All entries in
col1are consideredDISTINCTas it uses the Mimer SQL default collationISO8BIT:However, in this next statement:
SELECT DISTINCT colswe FROM table1;
colsweuses theswedish_1collation. `å' and `A' are considered to be distinct, but `v' and `W' are not:SELECT DISTINCT coleng FROM table1;
colenguses theenglish_1collation, `v' and `W' are considered to be distinct, but `å' and `A' are not:
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|