UNICODE (Transact-SQL)

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例Azure Synapse AnalyticsAnalytics Platform System (PDW)Microsoft Fabric 中的 SQL 分析终结点Microsoft Fabric 中的仓库

按照 Unicode 标准的定义,返回输入表达式的第一个字符的整数值。

Transact-SQL 语法约定

语法

UNICODE ( 'ncharacter_expression' )  

注意

若要查看 SQL Server 2014 (12.x) 及更早版本的 Transact-SQL 语法,请参阅早期版本文档

参数

'ncharacter_expression'
是 nchar 或 nvarchar 表达式

返回类型

int

备注

在 SQL Server 2012 (11.x) 之前的 SQL Server 版本以及 Azure SQL 数据库 中,UNICODE 函数返回范围 000000 - 00FFFF(它能够表示 Unicode 基本多文种平面 (BMP) 中的 65,535 个字符)内的 UCS-2 码位。 从 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 支持