Posts

Showing posts from September, 2016

DATALENGTH() in SQL Server

Image
In our previous blog, we have discussed the LEN() function in SQL Server.

In this blog, we are going to discuss the DATALENGTH() function in SQL Server.
DATALENGTH(): The DATALENGTH() function returns the number of bytes required/used to represent an expression.
Syntax:
DATALENGTH( expression )
Things to Remember:
1. DATALENGTH() function includes the trailing blank spaces while calculating the number of bytes required/used to represent an expression.2. The DATALENGTH() function return null, if the expression is null.3. The DATALENGTH() function return twice the number of characters required/used to represent an expression when the data type is nvarchar. Since nvarchar store 2 byte per character.
Practical Implementation: -
The following example illustrate how to use the function.
I) With ‘varchar’ data type
(i) Finding the DATALENGTH of any expression.
declare@varString1varchar(50)set@varString1='LIVEVIAR'select@varString1asStringValue,DATALENGTH(@varString1)asStringLength
Output:
Strin…