UNICODE (Transact-SQL)

適用於:Microsoft Fabric 中 Microsoft Fabric倉儲中的 SQL ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Azure SynapseAnalytics Analytics Platform System (PDW)SQL 分析端點

依照 Unicode 標準所定義,傳回輸入運算式第一個字元的整數值。

Transact-SQL 語法慣例

Syntax

UNICODE ( 'ncharacter_expression' )  

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

引數

'ncharacter_expression'
這是 ncharnvarchar 運算式。

傳回型別

int

備註

在 SQL Server 2012 (11.x) 之前的 SQL Server 版本與 Azure SQL Database 中,UNICODE 函式會傳回範圍介於 000000 到 00FFFF 之間的 UCS-2 字碼元素,此範圍可代表 Unicode 基本多語平面 (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

下列範例會利用 SUBSTRINGUNICODECONVERT 函數來列印 Å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 支援