UNICODE (Transact-SQL)

S’applique à :SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)Point de terminaison analytique SQL dans Microsoft FabricEntrepôt dans Microsoft Fabric

Renvoie la valeur entière, telle qu'elle est définie par la norme Unicode, du premier caractère de l'expression entrée.

Conventions de la syntaxe Transact-SQL

Syntaxe

UNICODE ( 'ncharacter_expression' )  

Remarque

Pour afficher la syntaxe Transact-SQL pour SQL Server 2014 (12.x) et versions antérieures, consultez la Documentation sur les versions antérieures.

Arguments

'ncharacter_expression'
Expression nchar ou nvarchar.

Types de retour

int

Notes

Dans les versions de SQL Server antérieure à SQL Server 2012 (11.x) et dans Azure SQL Database, la fonction UNICODE retourne un codepoint UCS-2 dans la plage de 000000 à 00FFFF qui est capable de représenter les 65 535 caractères dans le plan multilingue de base (BMP) Unicode. À partir de SQL Server 2012 (11.x), lors de l'utilisation de classements prenant en charge des Caractères supplémentaires (SC), UNICODE retourne un codepoint UTF-16 compris entre 000000 et 10FFFF. Pour plus d’informations sur le support Unicode dans le Moteur de base de données, consultez Classement et support Unicode.

Exemples

R. Utilisation d'UNICODE et de la fonction NCHAR

Cet exemple fait appel aux fonctions UNICODE et NCHAR pour imprimer la valeur UNICODE du premier caractère de la chaîne Åkergatan 24 et pour imprimer correctement le premier caractère, soit Å.

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

Voici le jeu de résultats obtenu.

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

B. Utilisation de SUBSTRING, UNICODE et CONVERT

Cet exemple fait appel aux fonctions SUBSTRING, UNICODE et CONVERT pour imprimer le nombre de caractères, le caractère Unicode et la valeur UNICODE de chacun des caractères de la chaîne Å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; 

Voici le jeu de résultats obtenu.

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  

Voir aussi

ASCII (Transact-SQL)
CHAR (Transact-SQL)
NCHAR (Transact-SQL)
Fonctions de chaîne (Transact-SQL)
Prise en charge d'Unicode et du classement