Skip to content

Commit 5e8d297

Browse files
authored
Merge pull request #7766 from pmasl/patch-314
Update nchar-and-nvarchar-transact-sql.md
2 parents 1d00e79 + 7e7e23f commit 5e8d297

1 file changed

Lines changed: 11 additions & 38 deletions

File tree

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "nchar and nvarchar (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "7/22/2017"
4+
ms.date: "10/22/2018"
55
ms.prod: sql
66
ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw"
77
ms.reviewer: ""
@@ -21,67 +21,40 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
2121
# nchar and nvarchar (Transact-SQL)
2222
[!INCLUDE[tsql-appliesto-ss2008-all-md](../../includes/tsql-appliesto-ss2008-all-md.md)]
2323

24-
Character data types that are either fixed-length, **nchar**, or variable-length, **nvarchar**. Starting with [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)], when a [Supplementary Character (SC)](../../relational-databases/collations/collation-and-unicode-support.md#Supplementary_Characters) enabled collation is used, these data types store [Unicode](../../relational-databases/collations/collation-and-unicode-support.md#Unicode_Defn) data and use the UNICODE UTF-16 character set. If a non-SC collation is specified, then these data types store Unicode string data and use the UNICODE UCS-2 character set.
24+
Character data types that are either fixed-length, **nchar**, or variable-length, **nvarchar**. Starting with [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)], when a [Supplementary Character (SC)](../../relational-databases/collations/collation-and-unicode-support.md#Supplementary_Characters) enabled collation is used, these data types store the full range of [Unicode](../../relational-databases/collations/collation-and-unicode-support.md#Unicode_Defn) character data and use the [UTF-16](http://www.wikipedia.org/wiki/UTF-16) character encoding. If a non-SC collation is specified, then these data types store only the subset of character data supported by the [UCS-2](http://www.wikipedia.org/wiki/Universal_Coded_Character_Set#Encoding_forms) character encoding.
2525

2626
## Arguments
2727
**nchar** [ ( n ) ]
28-
Fixed-length Unicode string data. *n* defines the string length and must be a value from 1 through 4,000. The storage size is two times *n* bytes. When the collation code page uses double-byte characters, the storage size is still *n* bytes. Depending on the string, the storage size of *n* bytes can be less than the value specified for *n*. The ISO synonyms for **nchar** are **national char** and **national character**.
28+
Fixed-length string data. *n* defines the string length in byte-pairs and must be a value from 1 through 4,000. The storage size is two times *n* bytes. For [UCS-2](http://www.wikipedia.org/wiki/UTF-16#U+0000_to_U+D7FF_and_U+E000_to_U+FFFF) encoding, the storage size is two times *n* bytes and the number of characters that can be stored is also *n*. For UTF-16 encoding, the storage size is still two times *n* bytes but the number of characters that can be stored may be smaller than *n* because Supplementary Characters use two byte-pairs (also called [surrogate-pair](http://www.wikipedia.org/wiki/UTF-16#U+010000_to_U+10FFFF)). The ISO synonyms for **nchar** are **national char** and **national character**.
2929

3030
**nvarchar** [ ( n | **max** ) ]
31-
Variable-length Unicode string data. *n* defines the string length and can be a value from 1 through 4,000. **max** indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times *n* bytes + 2 bytes. When the collation code page uses double-byte characters, the storage size is still *n* bytes + 2 bytes. Depending on the string, the storage size of *n* bytes can be less than the value specified for *n*. The ISO synonyms for **nvarchar** are **national char varying** and **national character varying**.
31+
Variable-length string data. *n* defines the string length in byte-pairs and can be a value from 1 through 4,000. **max** indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times *n* bytes + 2 bytes. For [UCS-2](http://www.wikipedia.org/wiki/UTF-16#U+0000_to_U+D7FF_and_U+E000_to_U+FFFF) encoding, the storage size is two times *n* bytes + 2 bytes and the number of characters that can be stored is also *n*. For UTF-16 encoding, the storage size is still two times *n* bytes + 2 bytes but the number of characters that can be stored may be smaller than *n* because Supplementary Characters use two byte-pairs (also called [surrogate-pair](http://www.wikipedia.org/wiki/UTF-16#U+010000_to_U+10FFFF)). The ISO synonyms for **nvarchar** are **national char varying** and **national character varying**.
3232

3333
## Remarks
3434
When *n* is not specified in a data definition or variable declaration statement, the default length is 1. When *n* is not specified with the CAST function, the default length is 30.
3535

36-
If you use **char** or **varchar**, we recommend the following:
36+
If you use **nchar** or **nvarchar**, we recommend to:
3737
- Use **nchar** when the sizes of the column data entries are consistent.
3838
- Use **nvarchar** when the sizes of the column data entries vary considerably.
39-
- Use **nvarchar(max)** when the sizes of the column data entries vary considerably, and the size might exceed 8,000 bytes.
39+
- Use **nvarchar(max)** when the sizes of the column data entries vary considerably, and the string length might exceed 4,000 byte-pairs.
4040

4141
**sysname** is a system-supplied user-defined data type that is functionally equivalent to **nvarchar(128)**, except that it is not nullable. **sysname** is used to reference database object names.
4242

4343
Objects that use **nchar** or **nvarchar** are assigned the default collation of the database unless a specific collation is assigned using the COLLATE clause.
4444

4545
SET ANSI_PADDING is always ON for **nchar** and **nvarchar**. SET ANSI_PADDING OFF does not apply to the **nchar** or **nvarchar** data types.
4646

47-
Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.
47+
Prefix a Unicode character string constants with the letter N to signal UCS-2 or UTF-16 input, depending on whether an SC collation is used or not. Without the N prefix, the string is converted to the default code page of the database that may not recognize certain characters. Starting with [!INCLUDE[sql-server-2019](../../includes/sssqlv15-md.md)], when a UTF-8 enabled collation is used, the default code page is capable of storing UNICODE UTF-8 character set.
4848

4949
> [!NOTE]
50-
> When prefixing a string constant with the letter N, the implicit conversion will result in a Unicode string if the constant to convert does not exceed the max length for a Unicode string data type (4,000). Otherwise, the implicit conversion will result in a Unicode large-value (max).
50+
> When prefixing a string constant with the letter N, the implicit conversion will result in a UCS-2 or UTF-16 string if the constant to convert does not exceed the max length for the nvarchar string data type (4,000). Otherwise, the implicit conversion will result in a large-value nvarchar(max).
5151
5252
> [!WARNING]
53-
> Each non-null **varchar(max)** or **nvarchar(max)** column requires 24 bytes of additional fixed allocation, which counts against the 8,060-byte row limit during a sort operation. These additional bytes can create an implicit limit to the number of non-null **varchar(max)** or **nvarchar(max)** columns in a table. No special error is provided when the table is created (beyond the usual warning that the maximum row size exceeds the allowed maximum of 8060 bytes) or at the time of data insertion. This large row size can cause errors (such as error 512) that users may not anticipate during some normal operations. Two examples of operations are a clustered index key update, or sorts of the full column set.
53+
> Each non-null **varchar(max)** or **nvarchar(max)** column requires 24 bytes of additional fixed allocation, which counts against the 8,060-byte row limit during a sort operation. These additional bytes can create an implicit limit to the number of non-null **varchar(max)** or **nvarchar(max)** columns in a table. No special error is provided when the table is created (beyond the usual warning that the maximum row size exceeds the allowed maximum of 8,060 bytes) or at the time of data insertion. This large row size can cause errors (such as error 512) that users may not anticipate during some normal operations. Two examples of operations are a clustered index key update, or sorts of the full column set.
5454
5555
## Converting Character Data
5656
For information about converting character data, see [char and varchar (Transact-SQL)](../../t-sql/data-types/char-and-varchar-transact-sql.md).
5757

58-
## Examples
59-
60-
```sql
61-
CREATE TABLE dbo.MyTable
62-
(
63-
MyNCharColumn nchar(15)
64-
,MyNVarCharColumn nvarchar(20)
65-
66-
);
67-
68-
GO
69-
INSERT INTO dbo.MyTable VALUES (N'Test data', N'More test data');
70-
GO
71-
SELECT MyNCharColumn, MyNVarCharColumn
72-
FROM dbo.MyTable;
73-
```
74-
75-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
76-
77-
```sql
78-
MyNCharColumn MyNVarCharColumn
79-
--------------- --------------------
80-
Test data More test data
81-
82-
(1 row(s) affected)
83-
```
84-
8558
## See also
8659
[ALTER TABLE (Transact-SQL)](../../t-sql/statements/alter-table-transact-sql.md)
8760
[CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)
@@ -92,6 +65,6 @@ Test data More test data
9265
[LIKE (Transact-SQL)](../../t-sql/language-elements/like-transact-sql.md)
9366
[SET ANSI_PADDING (Transact-SQL)](../../t-sql/statements/set-ansi-padding-transact-sql.md)
9467
[SET @local_variable (Transact-SQL)](../../t-sql/language-elements/set-local-variable-transact-sql.md)
95-
[Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
96-
68+
[Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md)
69+
[Single-Byte and Multibyte Character Sets](/cpp/c-runtime-library/single-byte-and-multibyte-character-sets)
9770

0 commit comments

Comments
 (0)