Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am currently using "sql server 2012" .

i have tried an effort of Concatenat two char type of variable ...

DECLARE @FirstName Char(20) = 'ruchrmitl',
@LastName Char(20) = 'kolkata'

if we find how much space it has been taken in db ... than ..

SQL
SELECT DATALENGTH(@FirstName) CharSpaceUsed1,
DATALENGTH(@LastName) CharSpaceUsed2


after executing query it will give result ...
VB
CharSpaceUsed1
20
VarCharSpaceUsed2
20


till yet all is ok



but after concatenate if i saw length , it's surprising to me ...

SQL
SELECT  @FirstName + ' ' + @LastName AS Name,
 len(@FirstName + ' ' + @LastName) AS Length



it will give result as ...

Name Length
ruchrmitl kolkata 28


how it's length become 28 .

can any one explain 2 me , thanx in advanse ...
Posted

There is a bit difference between char and varchar[^] data types:
char [ ( n ) ]
    Fixed-length, non-Unicode character data with a length of n bytes. n must be a value from 1 through 8,000. The storage size is n bytes. The SQL-2003 synonym for char is character.

varchar [ ( n | max ) ]
    Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. The SQL-2003 synonyms for varchar are char varying or character varying.


So, when you use LEN[^] function, it returns the number of characters of the specified string expression, excluding trailing blanks, which means: LEN(@FirstName + ' ' + @LastName)=28, because trailing blanks of @FirstName are not cutted, ' ' (space) length is equal to 1 and length of @LastName is equal to 7 (trailing blanks are removed)!
The statement:
LEN(@FirstName + ' ' + @LastName)
is not equal to:
LEN(@FirstName) + LEN(' ') + LEN(@LastName)
 
Share this answer
 
v3
Comments
DamithSL 2-Dec-14 3:28am    
yes, 5+
Maciej Los 2-Dec-14 3:40am    
Thank you ;)
DATALENGTH Returns the number of bytes used to represent any expression. so in your first sql statement 20 means 20 bytes in your Char(20) fields.

but len(@FirstName + ' ' + @LastName) give you number of characters of the specified string expression, excluding trailing blanks.

So your @LastName trailing spaces will be removed when you use LEN function
@FirstName + ' ' + @LastName = 20 + 1 + 7 =28


refer :
DATALENGTH [^]
LEN [^]
 
Share this answer
 
v5
Comments
/\jmot 2-Dec-14 1:08am    
+5
/\jmot 2-Dec-14 1:08am    
i didn't see your answer before posting mine.
GDdixit 2-Dec-14 1:31am    
hi , you have specified here that length of @firstname is =20. and for space it's one and for @lastname it's 7 ...

but if we use len function for finding their lenght individually than ...

SELECT LEN(@FirstName) CharSpaceUsed,
LEN(@LastName) VarCharSpaceUsed

it will gives length of @firstname is 9 and for @lastname is 7
and as you specified length above for @firstname is 20 . it's the bytes used by this variable ...


it must be 9+1+7=17
DamithSL 2-Dec-14 1:37am    
No,
LEN(@FirstName) + LEN(' ') + LEN(@LastName) = 9+1+7= 17
that is not equal to
LEN(@FirstName + ' ' + @LastName)
note that below expression having string with some white spaces after first name and those will not ignored by using len function
@FirstName + ' ' + @LastName
equal to
"ruchrmitl........kolkata........"
dots means empty space
Len function will get only "ruchrmitl........kolkata" part, neglect trailing spaces. hope this will help you to understand what is the reason.
Maciej Los 2-Dec-14 2:25am    
You're right!
I explained it in my answer ;)
C#
DATALENGTH :Returns the number of bytes used to represent any expression.
LEN :Returns the number of characters of the specified string expression, excluding trailing blanks.

see this, you'll get the answer..
http://msdn.microsoft.com/en-us/library/ms173486.aspx[^]
http://msdn.microsoft.com/en-us/library/ms190329.aspx[^]
 
Share this answer
 
Its beacuse you have delared as Char.
In SQL Char data type is fixed length data type.iots better you can use Varchar datatype See your query below :

SQL
DECLARE @FirstName Char(20) = 'ruchrmitl',
@LastName Char(20) = 'kolkata' 
DECLARE @FirstName1 varchar(20) = 'ruchrmitl',
@LastName1 varchar(20) = 'kolkata'

 SELECT  @FirstName1 + '' + @LastName1 AS Name,len(@FirstName1),len(@LastName1),
 len(@FirstName1 + ' ' + @LastName1) AS Length ,len(CONCAT (@FirstName1,@LastName1))
 
 SELECT  @FirstName + '' + @LastName AS Name,len(@FirstName),len(@LastName),
 len(@FirstName + ' ' + @LastName) AS Length ,len(CONCAT (@FirstName,@LastName))
 
Share this answer
 
Comments
GDdixit 2-Dec-14 1:33am    
no , char have not fixed length , its' have fixed bytes that it taken ..

if we find it's length individually by using len function it will give 9 and 7 respectively for @firstname and @lastname ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900