REVERSE (Transact-SQL)REVERSE (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 reverse order of a string value.

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

语法Syntax

REVERSE ( string_expression )  

备注

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

参数Arguments

string_expressionstring_expression
string_expression 是字符串或二进制数据类型的表达式string_expression is an expression of a string or binary data type. string_expression 可以是常量、变量,也可以是字符列或二进制数据列。string_expression can be a constant, variable, or column of either character or binary data.

返回类型Return Types

varchar 或 nvarcharvarchar or nvarchar

备注Remarks

string_expression 的数据类型必须可隐式转换为 varchar。string_expression must be of a data type that is implicitly convertible to varchar. 否则,请使用 CAST 显式转换 string_expression。Otherwise, use CAST to explicitly convert string_expression.

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

使用 SC 排序规则时,REVERSE 函数将不反转代理项对的两部分的顺序。When using SC collations, the REVERSE function will not reverse the order of two halves of a surrogate pair.

示例Examples

以下示例返回字符被反转的所有联系人的名字。The following example returns all contact first names with the characters reversed. 此示例使用 AdventureWorks2012AdventureWorks2012 数据库。This example uses the AdventureWorks2012AdventureWorks2012 database.

SELECT FirstName, REVERSE(FirstName) AS Reverse  
FROM Person.Person  
WHERE BusinessEntityID < 5  
ORDER BY FirstName;  
GO  

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

FirstName      Reverse
-------------- --------------
Ken            neK
Rob            boR
Roberto        otreboR
Terri          irreT

(4 row(s) affected)

以下示例反转变量中的字符。The following example reverses the characters in a variable.

DECLARE @myvar VARCHAR(10);  
SET @myvar = 'sdrawkcaB';  
SELECT REVERSE(@myvar) AS Reversed ;  
GO  

以下示例从 int 数据类型隐式转换为 varchar 数据类型,然后反转结果。The following example makes an implicit conversion from an int data type into varchar data type and then reverses the result.

SELECT REVERSE(1234) AS Reversed ;  
GO  

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

以下示例返回所有数据库的名称,以及字符被反转的名称。The following example returns names of all databases, and the names with the characters reversed.

SELECT name, REVERSE(name) FROM sys.databases;  
GO  

另请参阅See Also

CONCAT (Transact-SQL)CONCAT (Transact-SQL)
CONCAT_WS (Transact-SQL)CONCAT_WS (Transact-SQL)
FORMATMESSAGE (Transact-SQL)FORMATMESSAGE (Transact-SQL)
QUOTENAME (Transact-SQL)QUOTENAME (Transact-SQL)
REPLACE (Transact-SQL)REPLACE (Transact-SQL)
STRING_AGG (Transact-SQL)STRING_AGG (Transact-SQL)
STRING_ESCAPE (Transact-SQL)STRING_ESCAPE (Transact-SQL)
STUFF (Transact-SQL)STUFF (Transact-SQL)
TRANSLATE (Transact-SQL)TRANSLATE (Transact-SQL)
CAST 和 CONVERT (Transact-SQL) CAST and CONVERT (Transact-SQL)
数据类型 (Transact-SQL) Data Types (Transact-SQL)
字符串函数 (Transact-SQL)String Functions (Transact-SQL)