REVERSE (Transact-SQL)REVERSE (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回字串值的反轉順序。Returns the reverse order of a string value.

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

語法Syntax

REVERSE ( string_expression )  

引數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

varcharnvarcharvarchar or nvarchar

RemarksRemarks

string_expression 必須是可以隱含轉換成 varchar 的資料類型。string_expression must be of a data type that is implicitly convertible to varchar. 否則,請使用 CAST 來明確轉換 string_expressionOtherwise, use CAST to explicitly convert string_expression.

補充字元 (Surrogate 字組)Supplementary Characters (Surrogate Pairs)

使用 SC 定序時,REVERSE 函數不會反轉 Surrogate 字組兩半的順序。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 SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse 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)