Datalength Function In Sql in SQL
? DATALENGTH Function in SQL
The DATALENGTH() function returns the number of bytes used to store a value in a column or expression.
? Syntax
DATALENGTH(expression)expression: The column or value you want to measure.
? What it Returns
Number of bytes used to store the data, including trailing spaces for
CHARandVARCHAR.Different from
LEN()which returns the number of characters (ignoring trailing spaces).
? Example (SQL Server)
SELECT DATALENGTH('Hello World') AS DataLength; -- Returns 11 (bytes)SELECT DATALENGTH(CAST('Hello' AS VARCHAR(10))) AS DataLength; -- Returns 5SELECT DATALENGTH(CAST('Hello' AS NVARCHAR(10))) AS DataLength; -- Returns 10 (2 bytes per character)? Use Cases
To check storage size of data.
Useful for binary, varbinary, or string data to find actual bytes used.
If you want examples for other SQL dialects or differences between DATALENGTH() and LEN(), just ask!