|
Posted by Hugo Kornelis on 10/14/06 21:05
On 13 Oct 2006 20:13:34 -0700, adamwalan@yahoo.com wrote:
>Why this always returning 'AAA'
>
>declare @ss char(20)
>
>set @ss='AAA'
>
>set @ss=@ss+'BBB'
>print @ss
Hi adamwalan,
Ed Murphy already supplied two fixes. The answer to the question "why"
is that char(20) is fixed-length - it's always 20 position. So if you
assign 'AAA', SQL Server pads it with 17 spaces. Then you concatenate
'BBB' to it, to get a 23-character string 'AAA (etc) BBB' - and when
you assign that back to @ss, the rightmost three bytes (the 'BBB' part)
is trimmed off.
So technically, SQL Server isn'r returning 'AAA', but 'AAA (etc) '.
And here's a short repro that clearly shows the intermediate result
before @ss+'BBB' gets trimmed for assigning it to the char(20) column.
declare @ss char(20)
set @ss='AAA'
print @ss+'BBB'
--
Hugo Kornelis, SQL Server MVP
Navigation:
[Reply to this message]
|