RIGHT (Transact-SQL)RIGHT (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
文字列の右端から指定された数の文字を返します。Returns the right part of a character string with the specified number of characters.
Transact-SQL 構文表記規則
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 の型が binary か 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 のサロゲート ペアが 1 文字としてカウントされます。When using SC collations, the RIGHT function counts a UTF-16 surrogate pair as a single character. 詳細については、「 Collation and Unicode Support」を参照してください。For more information, see Collation and Unicode Support.
例Examples
A:列を指定して RIGHT を使用するA: Using RIGHT with a column
次の例では、AdventureWorks2012AdventureWorks2012 データベース内の各担当者の名前の右端から 5 文字が返されます。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 WarehouseParallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and Parallel Data WarehouseParallel Data Warehouse
B.B. 列を指定して RIGHT を使用するUsing RIGHT with a column
次の例では、DimEmployee
テーブル内の各姓の右端から 5 文字が返されます。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. 文字列を指定して RIGHT を使用するUsing RIGHT with a character string
次の例では、RIGHT
を使用して abcdefg
という文字列の右端の 2 文字を返します。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)