views:

316

answers:

3

Hi,

I need to concatenate 2 ntext columns into one. I can't convert them to nchar, cause both contains strings longer than 4000 chars. Is there a way to do this in SQL Server 2005?

A: 

Have you tried the + operator?

update theTable
set newField = oldField1 + oldField2
Guffa
Tried and got errors.
MariusCC
@MariusCC: Could you be more specific than just "errors"?
Guffa
+3  A: 

Convert them to nvarchar(max) for the concatentation. It's the SQL 2005 replacement for ntext and allows all the usual nvarchar operations.

AakashM
Can't convert them because of risk of breaking the app: MS TFS. I'm just trying to hack some fields.
MariusCC
+3  A: 
UPDATE 
    YourTable
SET 
    Field = CAST( (CAST(field1 AS NVARCHAR(MAX)) + CAST(field2 AS NVARCHAR(MAX))) AS NEXT)
WHERE 
    (your condition here)

But really - with SQL Server 2005, NTEXT becomes deprecated and will most likely be phased out in SQL Server 2008 R2 or one release later. NVARCHAR(MAX) is the logical successor, giving you all NTEXT ever gave you, and a lot more!

If your fields would be NVARCHAR(MAX) from the beginning, you could just write:

UPDATE 
    YourTable
SET 
    field = field1 + field2
WHERE 
    (your condition here)

and be done with it!

I'd suggest you upgrade your tables to use NVARCHAR(MAX) instead of NTEXT.

Marc

marc_s
Hi Marc. I don't want to try to convert them because of risk of breaking the app: MS Team Foundation Server.I have enough misery without 'fixing' it. Thanks for your solution. Worked like magic ;).
MariusCC
OK, good point - never touch a running system :-)
marc_s