Click to See Complete Forum and Search --> : SQL Server and Concatenation


ar_ogsys
October 12th, 2005, 01:36 PM
Hey,

I'm trying to concatenate query results into a char variable, but for some reason the query results won't concatenate. For example:

DECLARE @COMP CHAR(12)
DECLARE @COMP2 char(12)
DECLARE @COMP3 char(12)

SET @COMP3 = (SELECT Code FROM Company CO WHERE CO.Code = 3)
SET @COMP2 = (SELECT Code FROM Company CO WHERE CO.Code = 2)
PRINT @COMP3
PRINT @COMP2
SET @COMP = @COMP2 + @COMP3
PRINT @COMPWhere CO.Code is also char(12). The output looks like:
3
2
2

Are the select statements putting NULLs around my variables? If so, is there a way to clean the data/variables?

Or is there some other reason this code won't concatenate?

I'm using SQL Server and SQL Query Analyzer.

Thanks for any help!

Eli Gassert
October 12th, 2005, 01:46 PM
When I use VarChar instead of Char, it works just fine.

I rarely use Char so I don't know what the deal is with the + operator on it. But if you're able to use VarChar in this case, I would.

wildfrog
October 12th, 2005, 02:03 PM
You could try using the LTRIM(...) function.
SET @COMP = LTRIM(@COMP2) + LTRIM(@COMP3)
- petter

ar_ogsys
October 12th, 2005, 02:42 PM
Thanks guys! VARCHAR fixed the problem, though I've no idea why :P

Also, yes I failed to notice I was overflowing the buffer, so LTRIM() was fixed that part.