RIGHT (Transact-SQL)RIGHT (Transact-SQL)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse

返回字符串中从右边开始指定个数的字符。Returns the right part of a character string with the specified number of characters.

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

RIGHT ( character_expression , integer_expression )  

备注

若要查看 SQL Server 2014 及更早版本的 Transact-SQL 语法,请参阅早期版本文档To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

参数Arguments

character_expressioncharacter_expression
字符或二进制数据的表达式Is an expression of character or binary data. character_expression 可以是常量、变量或列。character_expression can be a constant, variable, or column. character_expression 可以是除 text 或 ntext 外的任何数据类型,可隐式转换为 varchar 或 nvarchar 。character_expression can be of any data type, except text or ntext, that can be implicitly converted to varchar or nvarchar. 否则,请使用 CAST 函数显式转换 character_expression。Otherwise, use the CAST function to explicitly convert character_expression.

备注

如果 string_expression 的类型为二进制或 varbinary ,则 RIGHT 将执行到 varchar 的隐式转换,因此不会保留二进制输入。If string_expression is of type binary or varbinary, RIGHT will perform an implicit conversion to varchar, and therefore will not preserve the binary input.

integer_expressioninteger_expression
指定要返回的 character_expression 的字符数的正整数。Is a positive integer that specifies how many characters of character_expression will be returned. 如果 integer_expression 为负,则返回错误。If integer_expression is negative, an error is returned. 如果 integer_expression 的数据类型为 bigint,且包含较大的值,则 character_expression 必须是较大的数据类型,如 varchar(max)。If integer_expression is type bigint and contains a large value, character_expression must be of a large data type such as varchar(max).

返回类型Return Types

character_expression 为非 Unicode 字符数据类型时,返回 varchar。Returns varchar when character_expression is a non-Unicode character data type.

character_expression 为 Unicode 字符数据类型时,返回 nvarchar。Returns nvarchar when character_expression is a Unicode character data type.

补充字符(代理项对)Supplementary Characters (Surrogate Pairs)

在使用 SC 排序规则时,RIGHT 函数将 UTF-16 代理项对计为一个字符。When using SC collations, the RIGHT function counts a UTF-16 surrogate pair as a single character. 有关详细信息,请参阅 排序规则和 Unicode 支持For more information, see Collation and Unicode Support.

示例Examples

答:对列使用 RIGHTA: Using RIGHT with a column

以下示例返回 AdventureWorks2012AdventureWorks2012 数据库中每个联系人名字中最右边的五个字符。The following example returns the five rightmost characters of the first name for each person in the AdventureWorks2012AdventureWorks2012 database.

SELECT RIGHT(FirstName, 5) AS 'First Name'  
FROM Person.Person  
WHERE BusinessEntityID < 5  
ORDER BY FirstName;  
GO  

下面是结果集:Here is the result set.

First Name  
----------  
Ken  
Terri  
berto  
Rob  
  
(4 row(s) affected)  
  

示例:Azure Synapse AnalyticsAzure Synapse Analytics并行数据仓库Parallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and 并行数据仓库Parallel Data Warehouse

B.B. 对列使用 RIGHTUsing RIGHT with a column

以下示例返回 DimEmployee 表中每个姓氏最右边的五个字符。The following example returns the five rightmost characters of each last name in the DimEmployee table.

-- Uses AdventureWorks  
  
SELECT RIGHT(LastName, 5) AS Name  
FROM dbo.DimEmployee  
ORDER BY EmployeeKey;  

以下为部分结果集。Here is a partial result set.

Name
-----
lbert
Brown
rello
lters

C.C. 对字符串使用 RIGHTUsing RIGHT with a character string

以下示例使用 RIGHT 返回字符串 abcdefg 中最左边的两个字符。The following example uses RIGHT to return the two rightmost characters of the character string abcdefg.

SELECT RIGHT('abcdefg', 2); 

下面是结果集:Here is the result set.

-------  
fg

另请参阅See Also

LEFT (Transact-SQL)LEFT (Transact-SQL)
LTRIM (Transact-SQL)LTRIM (Transact-SQL)
RTRIM (Transact-SQL)RTRIM (Transact-SQL)
STRING_SPLIT (Transact-SQL)STRING_SPLIT (Transact-SQL)
SUBSTRING (Transact-SQL)SUBSTRING (Transact-SQL)
TRIM (Transact-SQL)TRIM (Transact-SQL)
CAST 和 CONVERT (Transact-SQL) CAST and CONVERT (Transact-SQL)
数据类型 (Transact-SQL) Data Types (Transact-SQL)
字符串函数 (Transact-SQL)String Functions (Transact-SQL)