Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 DATALENGTH and LEN()

Author  Topic 

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2005-09-21 : 05:44:00
What is the difference between len() and DATALENGTH. I tried both in a SELECT, both are returning the same value. Can someone give me an example?

------------------------
I think, therefore I am - Rene Descartes

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-21 : 05:55:29
Len will trim the data but DATALENGTH doesnt do it

Select Len('Test ')
Select DATALENGTH ('Test ')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2005-09-21 : 05:58:28
DATALENGTH returns the length of the string in bytes, including trailing spaces. LEN returns the length in characters, excluding trailing spaces. For example,

SELECT
LEN('string'),
LEN('string '),
DATALENGTH('string'),
DATALENGTH('string '),
LEN(N'string'),
LEN(N'string '),
DATALENGTH(N'string'),
DATALENGTH(N'string ')

will return 6, 6, 6, 9, 6, 6, 12, 18
Go to Top of Page

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2005-09-21 : 11:55:28
Thank you guys! Solved my doubt.

------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-21 : 15:07:23
Added to Newbie Sticky

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210

Kristen
Go to Top of Page
   

- Advertisement -