Tuesday 29 October 2019

DATALENGTH () function in sql server


It returns the number of bytes which are used or required to represent an expression.
Syntax:
DATALENGTH(expression)
See the below example
 

Keep in mind it will return the bytes which take to store this data. In the above example I used varchar so it returned 6 if we use name as nvarchar then see what it will return.
 

If expression return NULL value then it will return null.
 

It includes the trailing blank spaces while calculating the number of bytes used/required to represent an expression. See below example
declare @name varchar(50);
declare @name1 nvarchar(50);
set @name ='    Bagesh  ';
set @name1='    Bagesh  ';

select DATALENGTH(@name) as varchar_length,
       DATALENGTH(@name1) as Nvarchar_lenght


           
\   

It support Text, NText and Image data type. See the below example.

                        



2 comments:

If you have any doubt, please let me know.

Popular Posts