UNICODE (Transact-SQL)

Se aplica a:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)Punto de conexión de análisis SQL en Microsoft FabricAlmacenamiento en Microsoft Fabric

Devuelve el valor entero, según la definición del estándar Unicode, para el primer carácter de la expresión de entrada.

Convenciones de sintaxis de Transact-SQL

Sintaxis

UNICODE ( 'ncharacter_expression' )  

Nota:

Para ver la sintaxis de Transact-SQL para SQL Server 2014 (12.x) y versiones anteriores, consulte Versiones anteriores de la documentación.

Argumentos

'ncharacter_expression'
Es una expresión nchar o nvarchar.

Tipos de valor devuelto

int

Observaciones

En las versiones de SQL Server anteriores a SQL Server 2012 (11.x) y en Azure SQL Database, la función UNICODE devuelve un punto de código UCS-2 en el intervalo de 000000 a 00FFFF que es capaz de representar los 65 535 caracteres en el plano multilingüe básico (BMP) de Unicode. A partir de SQL Server 2012 (11.x), cuando se usan intercalaciones con caracteres complementarios (SC) habilitados, UNICODE devuelve un punto de código UTF 16 en el intervalo de 000000 a 10FFFF. Para obtener más información sobre la compatibilidad con Unicode en Motor de base de datos, consulte Compatibilidad con la intercalación y Unicode.

Ejemplos

A. Utilizar las funciones UNICODE y NCHAR

En el ejemplo siguiente se usan las funciones UNICODE y NCHAR para imprimir el valor UNICODE del primer carácter de la cadena Åkergatan 24 e imprimir el primer carácter real (Å).

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

El conjunto de resultados es el siguiente:

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

B. Utilizar SUBSTRING, UNICODE y CONVERT

En el ejemplo siguiente se utilizan las funciones SUBSTRING, UNICODE y CONVERT para imprimir el número de carácter, el carácter Unicode y el valor UNICODE de cada uno de los caracteres de la cadena Å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; 

El conjunto de resultados es el siguiente:

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  

Consulte también

ASCII (Transact-SQL)
CHAR (Transact-SQL)
NCHAR (Transact-SQL)
String Functions (Transact-SQL) [Funciones de cadena (Transact-SQL)]
Compatibilidad con la intercalación y Unicode