Skip to content

DATALENGTH

  • expression: This refers to an expression of any type except for text, ntext, image, cursor, and timestamp. DATALENGTH() function is used to measure the length (in bytes) of the expression.
DECLARE @message nvarchar(50);
SET @message = 'Hello, SQL Server!';
SELECT DATALENGTH(@message) as length;
length
-----------
28

The DATALENGTH() function returns the number of bytes used to represent an expression’s value. Here, the string ‘Hello, SQL Server!’ returns 28. As we use nvarchar, each character is two bytes, so the length is double of the number of characters.