Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how does datalength work

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
Sorry for this question..

How do you determine the bytes of a string manually? For example, if the string is '1,2,3,4' how do you determine the bytes of that string?

Thanks.
 
There are a couple functions in SQL Server for determining the length of a string.

There is Len and DataLength

Len will tell you the number of characters (not bytes). DataLength will tell you the number of bytes needed to store the string.

For example.

Code:
Declare @Temp VarChar(20)
Set @Temp = '1,2,3,4'

Select Len(@Temp), DataLength(@Temp)

Declare @nTemp nVarChar(20)
Set @nTemp = '1,2,3,4'

Select Len(@nTemp), DataLength(@nTemp)


Len will remove trailing spaces for you too (while it is determining length).

Code:
Declare @Temp VarChar(20)
Set @Temp = '1,2,3,4   '

Select Len(@Temp), DataLength(@Temp)

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for your reply George..

My question was more can you figure out the byte of a string calculating it by hand.
 
I'm afraid I still don't understand what you are asking. Can you please elaborate?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
each character is 1 byte for varchar/char and 2 bytes for nvarchar/nchar

count the characters you have and multiply by 1 or 2 accordingly...

--------------------
Procrastinate Now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top