Click here to Skip to main content
11,706,799 members (56,144 online)
Click here to Skip to main content

Tagged as

Len() vs. Datalength()

, 27 Sep 2012 CPOL 8.1K 2
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
GeneralLEN ignores tailing spaces Pin
grimmuko18-Sep-14 8:40
membergrimmuko18-Sep-14 8:40 
GeneralMy vote of 4 Pin
aasim abdullah1-Oct-12 3:39
memberaasim abdullah1-Oct-12 3:39 
GeneralRe: My vote of 4 Pin
Arunprasath Natarajan1-Oct-12 19:59
memberArunprasath Natarajan1-Oct-12 19:59 
GeneralGood One Pin
aasim abdullah1-Oct-12 3:38
memberaasim abdullah1-Oct-12 3:38 
Learned somthing, thanks Arunprasath
GeneralRe: Good One Pin
Arunprasath Natarajan1-Oct-12 20:01
memberArunprasath Natarajan1-Oct-12 20:01 

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

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

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