DATALENGTH (Transact-SQL)DATALENGTH (Transact-SQL)

适用于: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: YesSQL Server YesAzure SQL Database YesAzure Synapse Analytics (SQL DW) YesParallel 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 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

DATALENGTH ( expression )   

参数Arguments

expressionexpression
任何数据类型的表达式An expression of any data type.

返回类型Return types

如果 expression 具有一个 nvarchar(max)、varbinary(max) 或 varchar(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:

  • 图像image
  • 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

此示例查找 Name 表中的 Product 列的长度: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)