22
Reply

Difference between LEN() and DATALENGTH() in sql server ?

Satyaprakash Samantaray

Satyaprakash Samantaray

Mar 07, 2017
4.4k
1

    Len will give you how many characters are there in data(Any data type) whereas DataLenth() will give you how much memory required to store the same data(in Bytes). We can observe the difference between them when checked with data types other than char/string.Len(2) = 1 Len(20) = 2 DataLength(2) / DataLength(20) / DataLength(200) = 4 .Equivalents in C#. Len() = string.Length() DataLength() = SizeOf()Try with other data types.

    Siva Tiyyagura
    April 05, 2017
    6

    DATALEN will return the number of bytes that are used to store the value LEN will return the number of characters in a string. Since a string can use single or double-byte characters, this differs from DATALENGTH in that you will always get 1, no matter how long a single character is

    Shalu Shalini
    April 01, 2017
    3

    Yes the LEN() will return the length of data(count of characters) whereas the DATALENGTH() will return the length of memory to store that data, the straight forward example of that isDeclare @data nvarchar(20) set @data='abc' select LEN(@data) as _LEN , DATALENGTH(@data) as _DATALENGTH you will get output as _LEN=3(length of data) and _DATALENGTH=6(length of memory to store @data), _DATALENGTH=6 because the nvarchar takes 2 bytes per char. Hope that will help you :)

    Harinder Kumar
    October 16, 2017
    1

    Yes the LEN() will return the length of data(count of characters) whereas the DATALENGTH() will return the length of memory to store that data, the straight forward example of that isDeclare @data nvarchar(20) set @data='abc' select LEN(@data) as _LEN , DATALENGTH(@data) as _DATALENGTH you will get output as _LEN=3(length of data) and _DATALENGTH=6(length of memory to store @data), _DATALENGTH=6 because the nvarchar takes 2 bytes per char. Hope that will help you :)

    Harinder Kumar
    October 16, 2017
    1

    Len is count characters with skip the blank space after character e.g. LEN('satya ') o/p = 5. But it will count blank space before start the string e.g. LEN(' satya') o/p = 6. DataLength is count blank spaces form both side gives output e.g. DATALENGTH(' satya ') o/p = 7.

    Sagar Sawale
    July 14, 2017
    1

    Rlease refer http://sqlhints.com/2015/07/18/difference-between-len-and-datalength-functions-in-sql-server/

    Srikanth Gogineni
    May 05, 2017
    1

    Check out these two links- https://docs.microsoft.com/en-us/sql/t-sql/functions/len-transact-sql https://docs.microsoft.com/en-us/sql/t-sql/functions/datalength-transact-sql

    Rohanjoy Bhattacharya
    March 31, 2017
    1

    Refer this link for more information: http://sqlhints.com/2015/07/18/difference-between-len-and-datalength-functions-in-sql-server/

    Paul
    March 29, 2017
    1

    LEN() counts no. of characters and it skips the blank spaces.e.g : SELECT LEN('Satya ') AS 'LEN o/p : 5DATALENGTH() counts no. of characters including the blank spaces.e.g : SELECT DATALENGTH('Satya ') AS 'DATALENGTH' o/p : 10

    In a very simple way both will tell you the length that you specify in that functionSELECT LEN('Testdata') AS 'LEN'LEN ------ 8SELECT DATALENGTH('Testdata') AS 'DATALENGTH'LEN ------ 8however the difference comes where we found the trailing spaces likeSELECT LEN('Testdata ') AS 'LEN'LEN ------ 8SELECT LEN('Testdata ') AS 'LEN'LEN ------ 18hope this will help..

    Amar Srivastava
    September 18, 2017
    0

    http://sqlhints.com/2015/07/18/difference-between-len-and-datalength-functions-in-sql-server/

    Jothish Kumar
    September 14, 2017
    0

    1.The LEN() Sql Server function returns the number of characters in the specified string expression where the DATALENGTH() Sql Server function returns the number of bytes used/required to represent an expression. 2. bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, in and bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise int.

    Jothish Kumar
    September 14, 2017
    0

    Len(): Gives length of expression or count of chars. DATALength(): it gives number of bytes I an expression.

    Krishna Lankireddy
    September 07, 2017
    0

    http://sqlhints.com/2015/07/18/difference-between-len-and-datalength-functions-in-sql-server/

    Rajkiran Swain
    September 01, 2017
    0

    LEN() DATALENGTH() DEFINITION The LEN() Sql Server function returns the number of characters in the specified string expression The DATALENGTH() Sql Server function returns the number of bytes used/required to represent an expression SYNTAX LEN ( string_expression ) DATALENGTH ( expression ) INPUT PARAMETER Input parameter is a string expression, it can be a constant or variable or column of character or binary data Input parameter is an expression of any data type RETURN TYPE bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise int EXAMPLE SELECT LEN('Shree') AS 'LEN' RESULT: LENSELECT DATALENGTH('Shree') AS 'DATALENGTH' RESULT: DATALENGTH

    Rajkiran Swain
    June 07, 2017
    0

    LEN() will return the number of characters in a string. Since a string can use single or double-byte characters, this differs from DATALENGTH in that you will always get 1, no matter how long a single character is.DATALEN will return the number of bytes that are used to store the value.

    Raj Kumar
    June 06, 2017
    0

    If you want to just get number of characters excluding blanks you would use LEN() function, while in all other cases DATALENGTH()

    Zahid Mustafa
    May 17, 2017
    0

    LEN() ->The LEN() Sql Server function returns the number of characters in the specified string expressionThe DATALENGTH() Sql Server function returns the number of bytes used/required to represent an expressionLEN ( string_expression )DATALENGTH ( expression )

    Rajkiran Swain
    May 15, 2017
    0

    Len() function means find out how many characters in string for any datatypes and Data length() is that to allow how much data are store inside data column(filed).

    Hitendra kumbhar
    May 09, 2017
    0

    LEN() function returns character it doesnot take whitespace DATALENGTH()function returns whitespace also eg: SELECT LEN('SUBASH ') output 6 SELECT DATALENGTH('SUBASH ') output 7

    Subash
    April 26, 2017
    0

    The LEN() Sql Server function returns the number of characters in the specified string expression The DATALENGTH() Sql Server function returns the number of bytes used/required to represent an expression

    Bidya Mishra
    April 19, 2017
    0

    The LEN() and DATALENGTH() functions are there for completely different purpose. But it often confuses many in identifying which one to use when. The aim of this article is to clear all these confusions by presenting a comparative analysis of LEN() and DATALENGTH() Sql Serer functions.

    Nihar Narendra
    April 13, 2017
    0