Transact-SQL 参考

NCHAR

根据 Unicode 标准所进行的定义,用给定整数代码返回 Unicode 字符。

语法

NCHAR ( integer_expression )

参数

integer_expression

介于 0 与 65535 之间的所有正整数。如果指定了超出此范围的值,将返回 NULL。

返回类型

nchar(1)

示例
A. 使用 NCHAR 和 UNICODE

下面的示例使用 UNICODE 和 NCHAR 函数打印字符串 Køenhavn 第二个字符的 UNICODE 值和 NCHAR(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øenhavn 的字符数、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, which 
-- 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're at, 
-- the actual Unicode character you're 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                 75          
                                          
----------- ----------------- ----------- 
2           ø                 248         
                                          
----------- ----------------- ----------- 
3           b                 98          
                                          
----------- ----------------- ----------- 
4           e                 101         
                                          
----------- ----------------- ----------- 
5           n                 110         
                                          
----------- ----------------- ----------- 
6           h                 104         
                                          
----------- ----------------- ----------- 
7           a                 97          
                                          
----------- ----------------- ----------- 
8           v                 118         
                                          
----------- ----------------- ----------- 
9           n                 110         
                                          
----------- ----------------- ----------- 
10          (null)            (null)      
                                          
----------- ----------------- ----------- 
11          (null)            (null)      
                                          
----------- ----------------- ----------- 
12          (null)            (null)      
                                          
----------- ----------------- ----------- 
13          (null)            (null)      
                                          
----------- ----------------- ----------- 
14          (null)            (null)      
                                          
----------- ----------------- ----------- 
15          (null)            (null)      
                                          
----------- ----------------- ----------- 
16          (null)            (null)      
                                          
----------- ----------------- ----------- 
17          (null)            (null)      
                                          
----------- ----------------- ----------- 
18          (null)            (null)

请参见

数据类型

字符串函数

UNICODE