Click here to Skip to main content
Click here to Skip to main content

Len() vs. Datalength()

By , 27 Sep 2012
 

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

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)

About the Author

Arunprasath Natarajan
Web Developer Thyrocare Technologies Limited
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberaasim abdullah1 Oct '12 - 3:39 
Yeah i was confused too Wink | ;)
GeneralRe: My vote of 4memberArunprasath Natarajan1 Oct '12 - 19:59 
Is it clear friend.Thumbs Up | :thumbsup:
GeneralGood Onememberaasim abdullah1 Oct '12 - 3:38 
Learned somthing, thanks Arunprasath
GeneralRe: Good OnememberArunprasath Natarajan1 Oct '12 - 20:01 
Happy if it helped you friend. Thanks.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Arunprasath Natarajan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid