SQL Server concatenation -- two string variables -- what am I missing?

Why doesn't this give expected result?

DECLARE @1 char(90);
DECLARE @2 char(90);
DECLARE @3 char(90);


Set @1 = 'first part';
set @2 = 'second part';

set @3 = @1 + @2;

print  @3;  -- outputs 'first part'  where did 2nd part go?

Upvotes: 2

Views: 5884

Answers (2)

ThinkingStiff
ThinkingStiff

Reputation: 65351

You can trim @1 and @2 and see more of your result (perhaps all of it if it fits into the CHAR(90) of @3).

SET @3 = RTRIM(@1) + RTRIM(@2);

Also, as @usr mentioned, you can make @3 larger to accommodate @1and @2. CHAR(180) would work as well as his suggestion of NVARCHAR (or VARCHAR).

Upvotes: 1

usr
usr

Reputation: 171178

First part is char(90) so it gets padded to fixed size 90 with spaces. @3 is also 90 chars so it is full just with the contents of @1. Switch to NVARCHAR(4000). Never use char except if you know what you're doing.

Upvotes: 2

Related Questions