DATALENGTH (Transact-SQL)DATALENGTH (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
この関数では、式を表すために必要なバイト数が返されます。This function returns the number of bytes used to represent any expression.
注意
文字列式内の文字数を取得するには、LEN 関数を使用します。To return the number of characters in a string expression, use the LEN function.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
DATALENGTH ( expression )
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
引数Arguments
式 (expression)expression
任意の型の式。An expression of any data type.
戻り値の型Return types
expression が varchar(max)、nvarchar(max)、varbinary(max) データ型の場合は bigint、それ以外の場合は int。bigint if expression has an nvarchar(max), varbinary(max), or varchar(max) data type; otherwise int.
解説Remarks
次のような可変長データを格納できるデータ型で使用すると、DATALENGTH
は非常に便利です。DATALENGTH
becomes really helpful when used with data types that can store variable-length data, such as:
- imageimage
- ntextntext
- nvarcharnvarchar
- texttext
- varbinaryvarbinary
- varcharvarchar
NULL 値の場合、DATALENGTH
は NULL を返します。For a NULL value, DATALENGTH
returns NULL.
注意
戻り値は、互換性レベルによって変わることがあります。Compatibility levels can affect return values. 互換性レベルの詳細については、「ALTER DATABASE 互換性レベル (Transact-SQL)」を参照してください。See ALTER DATABASE Compatibility Level (Transact-SQL) for more information about compatibility levels.
注意
特定の文字列式にエンコードされた文字数を取得するには LEN を使用し、特定の文字列式のバイト サイズを取得するには DATALENGTH を使用します。Use the LEN to return the number of characters encoded into a given string expression, and DATALENGTH to return the size in bytes for a given string expression. これらの出力は、データ型および列で使用されているエンコードの種類によっては、異なる場合があります。These outputs may differ depending on the data type and type of encoding used in the column. 異なる種類のエンコードでの記憶域の違いについて詳しくは、「照合順序と Unicode のサポート」をご覧ください。For more information on storage differences between different encoding types, see Collation and Unicode Support.
例Examples
この例では、Product
テーブルの Name
列の長さが返されます。This example finds the length of the Name
column in the Product
table:
USE AdventureWorks2016
GO
SELECT length = DATALENGTH(EnglishProductName), EnglishProductName
FROM dbo.DimProduct
ORDER BY EnglishProductName;
GO
関連項目See also
LEN (Transact-SQL)LEN (Transact-SQL)
CAST および CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)
データ型 (Transact-SQL)Data Types (Transact-SQL)
システム関数 (Transact-SQL)System Functions (Transact-SQL)