適用於:SQL Server
Azure SQL Database
Azure SQL 受控執行個體
Azure Synapse Analytics
Microsoft Fabric 中的 SQL 分析端點
Microsoft Fabric 中的倉庫
Microsoft Fabric 中的 SQL 資料庫
依照 Unicode 標準所定義,傳回含指定之整數碼的 Unicode 字元。
語法
NCHAR ( integer_expression )
引數
integer_expression
如果資料庫的定序不包含增補字元 (SC) 旗標,這會是從 0 到 65535 (0 到 0xFFFF) 的正整數。 如果指定了這個範圍以外的值,便會傳回 NULL。 如需增補字元的詳細資訊,請參閱定序和 Unicode 支援。
如果資料庫的定序支援 SC 旗標,這會是從 0 到 1114111 (0 到 0x10FFFF) 的正整數。 如果指定了這個範圍以外的值,便會傳回 NULL。
傳回型別
當預設資料庫定序不支援增補字元時,便為 nchar(1)。
當預設資料庫定序支援增補字元時,便為 nvarchar(2)。
如果參數 integer_expression 在 0 - 0xFFFF 範圍內,則只會傳回一個字元。 對於較高值,NCHAR 會傳回對應的 Surrogate 字組。 請勿使用 NCHAR(<High surrogate>) + NCHAR(\<Low Surrogate>) 建構 Surrogate 字組。 而應使用支援增補字元的資料庫定序,然後為 Surrogate 字組指定 Unicode 字碼指標。 下列範例示範建構 Surrogate 字組的舊有樣式方法,以及指定 Unicode 字碼指標的慣用方法。
CREATE DATABASE test COLLATE Finnish_Swedish_100_CS_AS_SC;
DECLARE @d NVARCHAR(10) = N'𣅿';
-- Old style method.
SELECT NCHAR(0xD84C) + NCHAR(0xDD7F);
-- Preferred method.
SELECT NCHAR(143743);
-- Alternative preferred method.
SELECT NCHAR(UNICODE(@d));
範例
A. 使用 NCHAR 和 UNICODE
下列範例會利用 UNICODE 和 NCHAR 函數來列印 UNICODE 字元字串第二個字元的 NCHAR 值和 København (Unicode 字元),以及列印實際的第二個字元 ø。
DECLARE @nstring NCHAR(8);
SET @nstring = N'København';
SELECT UNICODE(SUBSTRING(@nstring, 2, 1)),
NCHAR(UNICODE(SUBSTRING(@nstring, 2, 1)));
GO
結果集如下所示。
----------- -
248 ø
(1 row(s) affected)
B. 使用 SUBSTRING、UNICODE、CONVERT 和 NCHAR
下列範例會利用 SUBSTRING、UNICODE、CONVERT 和 NCHAR 函數來列印 København 字串中的字元數目、Unicode 字元,以及各字元的 UNICODE 值。
-- The @position variable holds the position of the character currently
-- being processed. The @nstring variable is the Unicode character
-- string to process.
DECLARE @position INT, @nstring NCHAR(9);
-- Initialize the current position variable to the first character in
-- the string.
SET @position = 1;
-- Initialize the character string variable to the string to process.
-- Notice that there is an N before the start of the string. This
-- indicates that the data following the N is Unicode data.
SET @nstring = N'København';
-- Print the character number of the position of the string you are at,
-- the actual Unicode character you are processing, and the UNICODE
-- value for this particular character.
PRINT 'Character #' + ' ' + 'Unicode Character' + ' ' + 'UNICODE Value';
WHILE @position <= DATALENGTH(@nstring)
BEGIN
SELECT @position,
NCHAR(UNICODE(SUBSTRING(@nstring, @position, 1))),
CONVERT(NCHAR(17), SUBSTRING(@nstring, @position, 1)),
UNICODE(SUBSTRING(@nstring, @position, 1))
SELECT @position = @position + 1
END;
GO
結果集如下所示。
Character # Unicode Character UNICODE Value
----------- ---- ----------------- -----------
1 K K 75
(1 row(s) affected)
----------- ---- ----------------- -----------
2 ø ø 248
(1 row(s) affected)
----------- ---- ----------------- -----------
3 b b 98
(1 row(s) affected)
----------- ---- ----------------- -----------
4 e e 101
(1 row(s) affected)
----------- ---- ----------------- -----------
5 n n 110
(1 row(s) affected)
----------- ---- ----------------- -----------
6 h h 104
(1 row(s) affected)
----------- ---- ----------------- -----------
7 a a 97
(1 row(s) affected)
----------- ---- ----------------- -----------
8 v v 118
(1 row(s) affected)
----------- ---- ----------------- -----------
9 n n 110
(1 row(s) affected)
----------- ---- ----------------- -----------
10 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
11 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
12 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
13 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
14 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
15 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
16 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
17 NULL NULL
(1 row(s) affected)
----------- ---- ----------------- -----------
18 NULL NULL
(1 row(s) affected)