UNICODE (Transact-SQL)UNICODE (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure Synapse Analytics (SQL DW) 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

依照 Unicode 標準所定義,傳回輸入運算式第一個字元的整數值。Returns the integer value, as defined by the Unicode standard, for the first character of the input expression.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

UNICODE ( 'ncharacter_expression' )  

引數Arguments

' ncharacter_expression '' ncharacter_expression '
這是 ncharnvarchar 運算式。Is an nchar or nvarchar expression.

傳回型別Return Types

intint

備註Remarks

SQL ServerSQL Server 之前的 SQL Server 2012 (11.x)SQL Server 2012 (11.x) 版本與 Azure SQL DatabaseAzure SQL Database 中,UNICODE 函式會傳回範圍介於 000000 到 00FFFF 之間的 UCS-2 字碼元素,此範圍可代表 Unicode 基本多語平面 (BMP) 中的 65,535 個字元。In versions of SQL ServerSQL Server earlier than SQL Server 2012 (11.x)SQL Server 2012 (11.x) and in Azure SQL DatabaseAzure SQL Database, the UNICODE function returns a UCS-2 codepoint in the range 000000 through 00FFFF which is capable of representing the 65,535 characters in the Unicode Basic Multilingual Plane (BMP). SQL Server 2012 (11.x)SQL Server 2012 (11.x) 開始,當使用支援補充字元 (SC) 的定序時,UNICODE 會傳回範圍 000000 到 10FFFF 之間的 UTF-16 字碼元素。Starting with SQL Server 2012 (11.x)SQL Server 2012 (11.x), when using Supplementary Character (SC) enabled collations, UNICODE returns a UTF-16 codepoint in the range 000000 through 10FFFF. 如需 Database EngineDatabase Engine 中 Unicode 支援的詳細資訊,請參閱定序和 Unicode 支援For more information on Unicode support in the Database EngineDatabase Engine, see Collation and Unicode Support.

範例Examples

A.A. 使用 UNICODE 和 NCHAR 函數Using UNICODE and the NCHAR function

下列範例會利用 UNICODENCHAR 函數來列印 Åkergatan 24 個字元字串中第一個字元的 UNICODE 值,以及列印實際的第一個字元 ÅThe following example uses the UNICODE and NCHAR functions to print the UNICODE value of the first character of the Åkergatan 24-character string, and to print the actual first character, Å.

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

以下為結果集。Here is the result set.

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

B.B. 使用 SUBSTRING、UNICODE 和 CONVERTUsing SUBSTRING, UNICODE, and CONVERT

下列範例會利用 SUBSTRINGUNICODECONVERT 函數來列印 Åkergatan 24 字串中的字元數目、Unicode 字元,以及每個字元的 UNICODE 值。The following example uses the SUBSTRING, UNICODE, and CONVERT functions to print the character number, the Unicode character, and the UNICODE value of each of the characters in the string Å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,   
      CONVERT(char(17), SUBSTRING(@nstring, @position, 1)),  
      UNICODE(SUBSTRING(@nstring, @position, 1));  
   SELECT @position = @position + 1;  
   END;  

以下為結果集。Here is the result set.

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  

另請參閱See Also

ASCII (Transact-SQL)ASCII (Transact-SQL)
CHAR (Transact-SQL)CHAR (Transact-SQL)
NCHAR (Transact-SQL) NCHAR (Transact-SQL)
字串函數 (Transact-SQL) String Functions (Transact-SQL)
定序與 Unicode 支援Collation and Unicode Support