NCHAR

Transact-SQL Reference

Transact-SQL Reference

NCHAR

Returns the Unicode character with the given integer code, as defined by the Unicode standard.

Syntax

NCHAR ( integer_expression )

Arguments

integer_expression

Is a positive whole number from 0 through 65535. If a value outside this range is specified, NULL is returned.

Return Types

nchar(1)

Examples
A. Use NCHAR and UNICODE

This example uses the UNICODE and NCHAR functions to print the UNICODE value and the NCHAR (Unicode character) of the second character of the København character string, and to print the actual second character, ø.

DECLARE @nstring nchar(8)
SET @nstring = N'København'
SELECT UNICODE(SUBSTRING(@nstring, 2, 1)), 
   NCHAR(UNICODE(SUBSTRING(@nstring, 2, 1)))
GO

Here is the result set:

----------- - 
248         ø

(1 row(s) affected)
B. Use SUBSTRING, UNICODE, CONVERT, and NCHAR

This example uses the SUBSTRING, UNICODE, CONVERT, and NCHAR functions to print the character number, the Unicode character, and the UNICODE value of each of the characters in the string København.

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

Here is the result set:

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)

See Also

Data Types

String Functions

UNICODE