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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Adding string variables together doesn't appear to work

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-02-28 : 08:07:29
Dan writes "When executing the following statements:

declare @x char(10), @y char(10)
set @x = 'abc'
set @y = @x + 'def'
select @x
select @y

the results are:

abc
abc

I expect @y should be equal to 'abcdef'. If I change the var types to int for example, then @y is summed correctly. Can anyone tell me why, or what I'm doing wrong? Thanks.

Dan"

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-28 : 08:11:06
Either:

declare @x varchar(10), @y varchar(10)


or

set @y = rtrim(@x) + 'def'


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

rlaubert
Yak Posting Veteran

96 Posts

Posted - 2007-02-28 : 15:53:39
The problem is you are using a fixed length variable and SQL adds spaces to the end of the value to fill out the fixed length field. As stated by snsql, using varchar will fix the problem.

Raymond Laubert
MCDBA, MCITP:Administration, MCT
Go to Top of Page
   

- Advertisement -