Tuesday, January 3, 2012

Len() and DataLength() function in SQL Server

Let me show the difference of the Len() and DataLength() function in sql server through a simple example.

Declare @TestTable Table
(
    ID        int,
    Name    varchar(50)
)

Insert Into @TestTable
values(1,'SQL Server')
     ,(2,'SQL Server ')
     ,(3,' SQL Server')
     ,(4,' SQL Server ')
   
Select Len(Name) As LengthOfString From @TestTable
Select DataLength(Name) As DataLengthOfString From @TestTable

Regarding the above sample query, the Len() function just avoiding the trailing space from the column data and returns the length of the string.But DataLength function returns the full length of column data , it won't avoid the trailing space.

No comments:

Post a Comment