DATALENGTH
DATALENGTH( expression )
Section titled “DATALENGTH( expression )”- 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.
Example
Section titled “Example”DECLARE @message nvarchar(50);SET @message = 'Hello, SQL Server!';SELECT DATALENGTH(@message) as length;Output
Section titled “Output”length-----------28Explanation
Section titled “Explanation”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.