UNICODE (Transact-SQL)

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)Microsoft Fabric の SQL 分析エンドポイントMicrosoft Fabric のウェアハウス

Unicode 標準に定義された、入力式の先頭文字の整数値を返します。

Transact-SQL 構文表記規則

構文

UNICODE ( 'ncharacter_expression' )  

Note

SQL Server 2014 (12.x) 以前のバージョンの Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。

引数

'ncharacter_expression'
nchar または nvarchar 式です。

戻り値の型

int

解説

SQL Server 2012 (11.x) より前のバージョンの SQL Server、および Azure SQL データベース では、UNICODE 関数で 000000 ~ 00FFFF の範囲の UCS-2 コードポイントが返されます。これにより、Unicode Basic Multilingual Plane (BMP) で 65,535 文字を表現できます。 SQL Server 2012 (11.x) 以降、補助文字 (SC) が有効になっている照合順序を使用すると、UNICODE では 000000 から 10FFFF の範囲の UTF-16 コードポイントが返されます。 データベース エンジンの Unicode サポートの詳細については、照合順序および Unicode サポートに関するページを参照してください。

A. UNICODE 関数と NCHAR 関数を使用する

次の例では、UNICODENCHAR の各関数を使用して、文字列 Åkergatan 24 の先頭文字を表す UNICODE 値と実際の先頭文字 Å を出力します。

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

結果セットは次のようになります。

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

B. SUBSTRING、UNICODE、CONVERT を使用する

次の例では、SUBSTRINGUNICODE、および CONVERT の各関数を使用して、文字列 Åkergatan 24 の各文字の文字番号、Unicode 文字、および UNICODE 値を出力します。

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

結果セットは次のようになります。

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  

参照

ASCII (Transact-SQL)
CHAR (Transact-SQL)
NCHAR (Transact-SQL)
文字列関数 (Transact-SQL)
照合順序と Unicode のサポート