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

Len() vs. Datalength()

, 27 Sep 2012
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)

About the Author

Arunprasath Natarajan
Web Developer Thyrocare Technologies Limited
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberaasim abdullah1-Oct-12 3:39 
Yeah i was confused too Wink | ;)
GeneralRe: My vote of 4 PinmemberArunprasath Natarajan1-Oct-12 19:59 
GeneralGood One Pinmemberaasim abdullah1-Oct-12 3:38 
GeneralRe: Good One PinmemberArunprasath 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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Arunprasath Natarajan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid