Skip to content

Commit 348f6e7

Browse files
authored
Merge pull request #7812 from pmasl/patch-327
Update collation-precedence-transact-sql.md
2 parents 683008c + 5422a4e commit 348f6e7

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

docs/t-sql/statements/collation-precedence-transact-sql.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Collation Precedence (Transact-SQL) | Microsoft Docs"
2+
title: "Collation Precedence | Microsoft Docs"
33
ms.custom: ""
44
ms.date: "03/15/2017"
55
ms.prod: sql
@@ -28,7 +28,7 @@ ms.author: carlrab
2828
manager: craigg
2929
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current"
3030
---
31-
# Collation Precedence (Transact-SQL)
31+
# Collation Precedence
3232
[!INCLUDE[tsql-appliesto-ss2008-all-md](../../includes/tsql-appliesto-ss2008-all-md.md)]
3333

3434
Collation precedence, also known as collation coercion rules, determines the following:
@@ -37,10 +37,10 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
3737

3838
- 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.
3939

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.
4141

4242
## 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.
4444

4545
|Collation label|Types of objects|
4646
|---------------------|----------------------|
@@ -50,11 +50,11 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
5050
|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.|
5151

5252
## 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.
5454

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.
5656

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:
5858

5959
- Explicit takes precedence over implicit. Implicit takes precedence over Coercible-default:
6060

@@ -76,7 +76,7 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
7676

7777
No-collation + Explicit X = Explicit
7878

79-
The following table summarizes the rules.
79+
The following table summarizes the rules.
8080

8181
|Operand coercion label|Explicit X|Implicit X|Coercible-default|No-collation|
8282
|----------------------------|----------------|----------------|------------------------|-------------------|
@@ -85,17 +85,17 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
8585
|**Coercible-default**|Result is Explicit X|Result is Implicit X|Result is Coercible-default|Result is No-collation|
8686
|**No-collation**|Result is Explicit X|Result is No-collation|Result is No-collation|Result is No-collation|
8787

88-
The following additional rules also apply to collation precedence:
88+
The following additional rules also apply to collation precedence:
8989

9090
- 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:
9191

9292
`WHERE ColumnA = ( 'abc' COLLATE French_CI_AS) COLLATE French_CS_AS`
9393

9494
- 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.
9595

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:
9797

98-
```
98+
```sql
9999
CREATE TABLE TestTab
100100
(PrimaryKey int PRIMARY KEY,
101101
CharCol char(10) COLLATE French_CI_AS
@@ -106,12 +106,12 @@ FROM TestTab
106106
WHERE CharCol LIKE N'abc'
107107
```
108108

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

111111
### Examples of Collation Rules
112112
The following examples show how the collation rules work. To run the examples, create the following test table.
113113

114-
```
114+
```sql
115115
USE tempdb;
116116
GO
117117

@@ -127,7 +127,7 @@ GO
127127
#### Collation Conflict and Error
128128
The predicate in the following query has collation conflict and generates an error.
129129

130-
```
130+
```sql
131131
SELECT *
132132
FROM TestTab
133133
WHERE GreekCol = LatinCol;
@@ -143,7 +143,7 @@ Cannot resolve collation conflict between 'Latin1_General_CS_AS' and 'Greek_CI_A
143143
#### Explicit Label vs. Implicit Label
144144
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.
145145

146-
```
146+
```sql
147147
SELECT *
148148
FROM TestTab
149149
WHERE GreekCol = LatinCol COLLATE greek_ci_as;
@@ -160,9 +160,9 @@ id GreekCol LatinCol
160160
```
161161

162162
#### 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.
164164

165-
```
165+
```sql
166166
SELECT (CASE WHEN id > 10 THEN GreekCol ELSE LatinCol END)
167167
FROM TestTab;
168168
```
@@ -174,7 +174,7 @@ Msg 451, Level 16, State 1, Line 1
174174
Cannot resolve collation conflict for column 1 in SELECT statement.
175175
```
176176

177-
```
177+
```sql
178178
SELECT PATINDEX((CASE WHEN id > 10 THEN GreekCol ELSE LatinCol END), 'a')
179179
FROM TestTab;
180180
```
@@ -186,7 +186,7 @@ Msg 446, Level 16, State 9, Server LEIH2, Line 1
186186
Cannot resolve collation conflict for patindex operation.
187187
```
188188

189-
```
189+
```sql
190190
SELECT (CASE WHEN id > 10 THEN GreekCol ELSE LatinCol END) COLLATE Latin1_General_CI_AS
191191
FROM TestTab;
192192
```
@@ -201,23 +201,23 @@ a
201201
```
202202

203203
## 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.
205205

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.
208208

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.
211211

212212
### 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.
214214

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.
216216

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.
218218

219219
### 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.
221221

222222
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.
223223

0 commit comments

Comments
 (0)