Partilhar via


UNICODE (Transact-SQL)

Aplica-se a:SQL ServerBase de Dados SQL do AzureInstância Gerida do Azure SQLAzure Synapse AnalyticsSistema de Plataforma de Análise (PDW)Ponto de Extremidade de Análise SQL no Microsoft FabricArmazém no Microsoft FabricBase de Dados SQL no Microsoft Fabric

Devolve o valor inteiro, conforme definido pelo padrão Unicode, para o primeiro carácter da expressão de entrada.

Transact-SQL convenções de sintaxe

Sintaxe

UNICODE ( 'ncharacter_expression' )  

Arguments

'ncharacter_expression'
É uma expressão nchar ou nvarchar .

Tipos de devolução

int

Observações

Em versões anteriores ao SQL Server 2012 (11.x) e no Azure SQL Database, a função UNICODE devolve um ponto de código UCS-2 no intervalo 000000 a 00FFFF, capaz de representar os 65.535 caracteres no Unicode Basic Multilingual Plane (BMP). A partir do SQL Server 2012 (11.x), ao usar colações ativadas por Caracteres Suplementares (SC), o UNICODE devolve um ponto de código UTF-16 no intervalo 000000 a 10FFFF. Para mais informações sobre o suporte Unicode no Motor de Base de Dados, consulte Collation e Suporte Unicode.

Examples

A. Utilização do UNICODE e da função NCHAR

O exemplo seguinte usa as UNICODE funções e NCHAR para imprimir o valor UNICODE do primeiro carácter da cadeia Åkergatan 24, e para imprimir o primeiro carácter real, Å.

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

Aqui está o conjunto de resultados.

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

B. Usando SUBSTRING, UNICODE e CONVERT

O exemplo seguinte usa as SUBSTRINGfunções , UNICODE, e CONVERT para imprimir o número de caracteres, o carácter Unicode e o valor UNICODE de cada um dos caracteres na cadeia Å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; 

Aqui está o conjunto de resultados.

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  

Ver também

ASCII (Transact-SQL)
CHAR (Transact-SQL)
NCHAR (Transact-SQL)
Funções de cadeia de caracteres (Transact-SQL)
Suporte a agrupamento e Unicode