Delen via


NCHAR (Transact-SQL)

Van toepassing op:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform Systeem (PDW)SQL-database in Microsoft Fabric

Geeft het Unicode-teken terug met de gespecificeerde gehele code, zoals gedefinieerd door de Unicode-standaard.

Transact-SQL syntaxis-conventies

Syntaxis

NCHAR ( integer_expression )  

Arguments

integer_expression
Wanneer de collatie van de database de Supplementary Character (SC) vlag niet bevat, is dit een positief geheel getal van 0 tot en met 65535 (0 tot en met 0xFFFF). Als een waarde buiten dit bereik wordt gespecificeerd, wordt NULL teruggegeven. Voor meer informatie over aanvullende tekens, zie Collatie en Unicode Support.

Wanneer de collatie van de database de SC-vlag ondersteunt, is dit een positief geheel getal van 0 tot 1114111 (0 tot 0x10FFFF). Als een waarde buiten dit bereik wordt gespecificeerd, wordt NULL teruggegeven.

Retourtypen

nchar(1) wanneer de standaard database-collatie geen aanvullende tekens ondersteunt.

nvarchar(2) wanneer de standaard database-collatie aanvullende tekens ondersteunt.

Als de parameter integer_expression in het bereik 0 - 0xFFFF ligt, wordt slechts één teken teruggegeven. Voor hogere waarden geeft NCHAR het bijbehorende surrogaatpaar terug. Bouw geen surrogaatpaar door gebruik te maken van NCHAR(<High surrogate>) + NCHAR(\<Low Surrogate>). Gebruik in plaats daarvan een database-collatie die aanvullende tekens ondersteunt en specificeer vervolgens het Unicode-codepunt voor het surrogaatpaar. Het volgende voorbeeld demonstreert zowel de oude methode om een surrogaatpaar te construeren als de voorkeursmethode om het Unicode-codepunt te specificeren.

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

Voorbeelden

Eén. Gebruik van NCHAR en UNICODE

Het volgende voorbeeld gebruikt de UNICODE en-functies NCHAR om de UNICODE waarde en het NCHAR (Unicode-teken) van het tweede teken van de København tekenreeks af te drukken, en om het daadwerkelijke tweede teken, ø.

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

Hier is het resultatenoverzicht.

----------- -   
248         ø  
(1 row(s) affected)  

B. Gebruik van SUBSTRING, UNICODE, CONVERT en NCHAR

Het volgende voorbeeld gebruikt de SUBSTRING, UNICODE, CONVERT, en NCHAR functies om het tekennummer, het Unicode-teken en de UNICODE-waarde van elk teken in de string Københavnaf te drukken.

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

Hier is het resultatenoverzicht.

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)  

Zie ook

ASCII (Transact-SQL)
CHAR (Transact-SQL)
UNICODE (Transact-SQL)
gegevenstypen (Transact-SQL)
tekenreeksfuncties (Transact-SQL)