UNICODE (Transact-SQL)

Si applica a:SQL Server database SQL di Azure Istanza gestita di SQL di Azure Azure Synapse Analytics AnalyticsPlatform System (PDW)SQL analytics endpoint in Microsoft FabricWarehouse in Microsoft Fabric

Restituisce il valore intero, come definito dallo standard Unicode, per il primo carattere dell'espressione di input.

Convenzioni di sintassi Transact-SQL

Sintassi

UNICODE ( 'ncharacter_expression' )  

Nota

Per visualizzare la sintassi Transact-SQL per SQL Server 2014 (12.x) e versioni precedenti, vedere la documentazione delle versioni precedenti.

Argomenti

'ncharacter_expression'
È un'espressione nchar o nvarchar.

Tipi restituiti

int

Osservazioni:

Nelle versioni di SQL Server precedenti a SQL Server 2012 (11.x) e nel database SQL di Azure, la funzione UNICODE restituisce un punto di codice UCS-2 nell'intervallo da 000000 a 00FFFF che è in grado di rappresentare i 65.535 caratteri nel piano BMP (Basic Multilingual Plane) Unicode. A partire da SQL Server 2012 (11.x), quando si usano regole di confronto abilitate per caratteri supplementari (SC), UNICODE restituisce un punto di codice UTF-16 nell'intervallo compreso tra 000000 e 10FFFF. Per altre informazioni sul supporto di Unicode nel motore di database, vedere Regole di confronto e supporto Unicode.

Esempi

R. Utilizzo delle funzioni UNICODE e NCHAR

Nell'esempio seguente vengono usate le funzioni UNICODE e NCHAR per stampare il valore UNICODE corrispondente al primo carattere della stringa Åkergatan 24 e per stampare il primo carattere effettivo, ovvero Å.

DECLARE @nstring NCHAR(12);  
SET @nstring = N'Åkergatan 24';  
SELECT UNICODE(@nstring), NCHAR(UNICODE(@nstring));  

Questo è il set di risultati.

----------- -   
197         Å  

B. Utilizzo di SUBSTRING, UNICODE e CONVERT

Nell'esempio seguente vengono utilizzate le funzioni SUBSTRING, UNICODE e CONVERT per stampare il numero di caratteri, il carattere Unicode e il valore UNICODE di ogni carattere della stringa Åkergatan 24.

-- 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(12);  
-- 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'Åkergatan 24';  
-- 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 <= LEN(@nstring)  
-- While these are still characters in the character string,  

BEGIN;  
   SELECT @position AS [position],   
      SUBSTRING(@nstring, @position, 1) AS [character],  
      UNICODE(SUBSTRING(@nstring, @position, 1)) AS [code_point];  
   SET @position = @position + 1;  
END; 

Questo è il set di risultati.

Character # Unicode Character UNICODE Value  
  
----------- ----------------- -----------   
1           Å                 197           
  
----------- ----------------- -----------   
2           k                 107           
  
----------- ----------------- -----------   
3           e                 101           
  
----------- ----------------- -----------   
4           r                 114           
  
----------- ----------------- -----------   
5           g                 103           
  
----------- ----------------- -----------   
6           a                 97            
  
----------- ----------------- -----------   
7           t                 116           
  
----------- ----------------- -----------   
8           a                 97            
  
----------- ----------------- -----------   
9           n                 110           
  
----------- ----------------- -----------   
10                            32            
  
----------- ----------------- -----------   
11          2                 50            
  
----------- ----------------- -----------   
12          4                 52  

Vedi anche

ASCII (Transact-SQL)
CHAR (Transact-SQL)
NCHAR (Transact-SQL)
Funzioni per i valori stringa (Transact-SQL)
Regole di confronto e supporto Unicode