You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- The collation that is used by collation-sensitive operators that use character string inputs but do not return a character string, such as LIKE and IN.
39
39
40
-
The collation precedence rules apply only to the character string data types: **char**, **varchar**, **text**, **nchar**, **nvarchar**, and **ntext**. Objects that have other data types do not participate in collation evaluations.
40
+
The collation precedence rules apply only to the character string data types: **char**, **varchar**, **text**, **nchar**, **nvarchar**, and **ntext**. Objects that have other data types do not participate in collation evaluations.
41
41
42
42
## Collation Labels
43
-
The following table lists and describes the four categories in which the collations of all objects are identified. The name of each category is called the collation label.
43
+
The following table lists and describes the four categories in which the collations of all objects are identified. The name of each category is called the collation label.
|No-collation|Indicates that the value of an expression is the result of an operation between two strings that have conflicting collations of the implicit collation label. The expression result is defined as not having a collation.|
51
51
52
52
## Collation Rules
53
-
The collation label of a simple expression that references only one character string object is the collation label of the referenced object.
53
+
The collation label of a simple expression that references only one character string object is the collation label of the referenced object.
54
54
55
-
The collation label of a complex expression that references two operand expressions with the same collation label is the collation label of the operand expressions.
55
+
The collation label of a complex expression that references two operand expressions with the same collation label is the collation label of the operand expressions.
56
56
57
-
The collation label of the final result of a complex expression that references two operand expressions with different collations is based on the following rules:
57
+
The collation label of the final result of a complex expression that references two operand expressions with different collations is based on the following rules:
58
58
59
59
- Explicit takes precedence over implicit. Implicit takes precedence over Coercible-default:
|**Coercible-default**|Result is Explicit X|Result is Implicit X|Result is Coercible-default|Result is No-collation|
86
86
|**No-collation**|Result is Explicit X|Result is No-collation|Result is No-collation|Result is No-collation|
87
87
88
-
The following additional rules also apply to collation precedence:
88
+
The following additional rules also apply to collation precedence:
89
89
90
90
- You cannot have multiple COLLATE clauses on an expression that is already an explicit expression. For example, the following `WHERE` clause is not valid because a `COLLATE` clause is specified for an expression that is already an explicit expression:
- Code page conversions for **text** data types are not allowed. You cannot cast a **text** expression from one collation to another if they have the different code pages. The assignment operator cannot assign values when the collation of the right text operand has a different code page than the left text operand.
95
95
96
-
Collation precedence is determined after data type conversion. The operand from which the resulting collation is taken can be different from the operand that supplies the data type of the final result. For example, consider the following batch:
96
+
Collation precedence is determined after data type conversion. The operand from which the resulting collation is taken can be different from the operand that supplies the data type of the final result. For example, consider the following batch:
97
97
98
-
```
98
+
```sql
99
99
CREATETABLETestTab
100
100
(PrimaryKey intPRIMARY KEY,
101
101
CharCol char(10) COLLATE French_CI_AS
@@ -106,12 +106,12 @@ FROM TestTab
106
106
WHERE CharCol LIKE N'abc'
107
107
```
108
108
109
-
The Unicode data type of the simple expression `N'abc'` has a higher data type precedence. Therefore, the resulting expression has the Unicode data type assigned to `N'abc'`. However, the expression `CharCol` has a collation label of Implicit, and `N'abc'` has a lower coercion label of Coercible-default. Therefore, the collation that is used is the `French_CI_AS` collation of `CharCol`.
109
+
The Unicode data type of the simple expression `N'abc'` has a higher data type precedence. Therefore, the resulting expression has the Unicode data type assigned to `N'abc'`. However, the expression `CharCol` has a collation label of Implicit, and `N'abc'` has a lower coercion label of Coercible-default. Therefore, the collation that is used is the `French_CI_AS` collation of `CharCol`.
110
110
111
111
### Examples of Collation Rules
112
112
The following examples show how the collation rules work. To run the examples, create the following test table.
113
113
114
-
```
114
+
```sql
115
115
USE tempdb;
116
116
GO
117
117
@@ -127,7 +127,7 @@ GO
127
127
#### Collation Conflict and Error
128
128
The predicate in the following query has collation conflict and generates an error.
129
129
130
-
```
130
+
```sql
131
131
SELECT*
132
132
FROM TestTab
133
133
WHERE GreekCol = LatinCol;
@@ -143,7 +143,7 @@ Cannot resolve collation conflict between 'Latin1_General_CS_AS' and 'Greek_CI_A
143
143
#### Explicit Label vs. Implicit Label
144
144
The predicate in the following query is evaluated in collation `greek_ci_as` because the right expression has the Explicit label. This takes precedence over the Implicit label of the left expression.
145
145
146
-
```
146
+
```sql
147
147
SELECT*
148
148
FROM TestTab
149
149
WHERE GreekCol = LatinCol COLLATE greek_ci_as;
@@ -160,9 +160,9 @@ id GreekCol LatinCol
160
160
```
161
161
162
162
#### No-Collation Labels
163
-
The `CASE` expressions in the following queries have a No-collation label; therefore, they cannot appear in the select list or be operated on by collation-sensitive operators. However, the expressions can be operated on by collation-insensitive operators.
163
+
The `CASE` expressions in the following queries have a No-collation label; therefore, they cannot appear in the select list or be operated on by collation-sensitive operators. However, the expressions can be operated on by collation-insensitive operators.
164
164
165
-
```
165
+
```sql
166
166
SELECT (CASE WHEN id >10 THEN GreekCol ELSE LatinCol END)
167
167
FROM TestTab;
168
168
```
@@ -174,7 +174,7 @@ Msg 451, Level 16, State 1, Line 1
174
174
Cannot resolve collation conflict for column 1 in SELECT statement.
175
175
```
176
176
177
-
```
177
+
```sql
178
178
SELECT PATINDEX((CASE WHEN id >10 THEN GreekCol ELSE LatinCol END), 'a')
179
179
FROM TestTab;
180
180
```
@@ -186,7 +186,7 @@ Msg 446, Level 16, State 9, Server LEIH2, Line 1
186
186
Cannot resolve collation conflict for patindex operation.
187
187
```
188
188
189
-
```
189
+
```sql
190
190
SELECT (CASE WHEN id >10 THEN GreekCol ELSE LatinCol END) COLLATE Latin1_General_CI_AS
191
191
FROM TestTab;
192
192
```
@@ -201,23 +201,23 @@ a
201
201
```
202
202
203
203
## Collation Sensitive and Collation Insensitive
204
-
Operators and functions are either collation sensitive or insensitive.
204
+
Operators and functions are either collation sensitive or insensitive.
205
205
206
-
Collation sensitive
207
-
This means that specifying a No-collation operand is a compile-time error. The expression result cannot be No-collation.
206
+
Collation sensitive
207
+
This means that specifying a No-collation operand is a compile-time error. The expression result cannot be No-collation.
208
208
209
-
Collation insensitive
210
-
This means that the operands and result can be No-collation.
209
+
Collation insensitive
210
+
This means that the operands and result can be No-collation.
211
211
212
212
### Operators and Collation
213
-
The comparison operators, and the MAX, MIN, BETWEEN, LIKE, and IN operators, are collation sensitive. The string used by the operators is assigned the collation label of the operand that has the higher precedence. The UNION operator is also collation sensitive, and all string operands and the final result is assigned the collation of the operand with the highest precedence. The collation precedence of the UNION operands and result are evaluated column by column.
213
+
The comparison operators, and the MAX, MIN, BETWEEN, LIKE, and IN operators, are collation sensitive. The string used by the operators is assigned the collation label of the operand that has the higher precedence. The UNION statement is also collation sensitive, and all string operands and the final result is assigned the collation of the operand with the highest precedence. The collation precedence of the UNION operand and result are evaluated column by column.
214
214
215
-
The assignment operator is collation insensitive and the right expression is cast to the left collation.
215
+
The assignment operator is collation insensitive and the right expression is cast to the left collation.
216
216
217
-
The string concatenation operator is collation sensitive, the two string operands and the result are assigned the collation label of the operand with the highest collation precedence. The UNION ALL and CASE operators are collation insensitive, and all string operands and the final results are assigned the collation label of the operand with the highest precedence. The collation precedence of the UNION ALL operands and result are evaluated column by column.
217
+
The string concatenation operator is collation sensitive, the two string operands and the result are assigned the collation label of the operand with the highest collation precedence. The UNION ALL and CASE statements are collation insensitive, and all string operands and the final results are assigned the collation label of the operand with the highest precedence. The collation precedence of the UNION ALL operands and result are evaluated column by column.
218
218
219
219
### Functions and Collation
220
-
THE CAST, CONVERT, and COLLATE functions are collation sensitive for **char**, **varchar**, and **text** data types. If the input and output of the CAST and CONVERT functions are character strings, the output string has the collation label of the input string. If the input is not a character string, the output string is Coercible-default and assigned the collation of the current database for the connection, or the database that contains the user-defined function, stored procedure, or trigger in which the CAST or CONVERT is referenced.
220
+
THE CAST, CONVERT, and COLLATE functions are collation sensitive for **char**, **varchar**, and **text** data types. If the input and output of the CAST and CONVERT functions are character strings, the output string has the collation label of the input string. If the input is not a character string, the output string is Coercible-default and assigned the collation of the current database for the connection, or the database that contains the user-defined function, stored procedure, or trigger in which the CAST or CONVERT is referenced.
221
221
222
222
For the built-in functions that return a string but do not take a string input, the result string is Coercible-default and is assigned either the collation of the current database, or the collation of the database that contains the user-defined function, stored procedure, or trigger in which the function is referenced.
0 commit comments