Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / SQL
Tip/Trick

Len() vs. Datalength()

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
27 Sep 2012CPOL 22.2K   2   7
Difference between Len() and Datalength().

Introduction

Many gets confused with Len() and Datalength(). Here I have given a small example to describe what is the main difference between them.

Using the code

SQL
DECLARE @Call_len VARCHAR(15)
DECLARE @Call_Dlen NVARCHAR(15)

SET @Call_len = 'Find Length'
SET @Call_Dlen = 'Find Length'

SELECT 
LEN(@Call_len) AS DATA1_LEN,
DATALENGTH(@Call_len) AS DATA1_DLEN,
LEN(@Call_Dlen) AS DATA2_LEN,
DATALENGTH(@Call_Dlen) AS DATA2_DLEN

Result:

DATA1_LEN	DATA1_DLEN	DATA2_LEN	DATA2_DLEN
    11              11              11              22 

In the above example, two variable have declared in two different datatypes. Where as Len() will count only the length of strings in the column and Datalength() will count the length of string as per datatype.

In short, LEN() is used to return no. of char. in string and DataLength() is used for no. of bytes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Thyrocare Technologies Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice Article Pin
Member 94147896-Aug-16 3:48
Member 94147896-Aug-16 3:48 
GeneralRe: Nice Article Pin
Arunprasath Natarajan9-Aug-16 21:12
Arunprasath Natarajan9-Aug-16 21:12 
GeneralLEN ignores tailing spaces Pin
grimmuko18-Sep-14 8:40
grimmuko18-Sep-14 8:40 
GeneralMy vote of 4 Pin
aasim abdullah1-Oct-12 3:39
aasim abdullah1-Oct-12 3:39 
GeneralRe: My vote of 4 Pin
Arunprasath Natarajan1-Oct-12 19:59
Arunprasath Natarajan1-Oct-12 19:59 
GeneralGood One Pin
aasim abdullah1-Oct-12 3:38
aasim abdullah1-Oct-12 3:38 
GeneralRe: Good One Pin
Arunprasath Natarajan1-Oct-12 20:01
Arunprasath Natarajan1-Oct-12 20:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.