9

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?

3 Answers 3

13
UPDATE 
    YourTable
SET 
    Field = CAST( (CAST(field1 AS NVARCHAR(MAX)) + CAST(field2 AS NVARCHAR(MAX))) AS NTEXT)
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

4
  • 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
    Aug 19, 2009 at 9:21
  • Hi @marc_s I think you might have a tiny little typo in your first example, "NEXT" rather than "NTEXT". (Good answer though, +1 from me) Nov 23, 2011 at 14:44
  • @AlexKey: yes, absolutely right! Thanks for catching that - updated my response
    – marc_s
    Nov 23, 2011 at 14:46
  • 1
    Hi Marc_s, If i have field1 as ntext and content is >4000 char. The command CAST(field1 AS NVARCHAR(MAX)) will auto TRIM field1. It not work
    – culithay
    Feb 6, 2015 at 8:51
3

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

1
  • Can't convert them because of risk of breaking the app: MS TFS. I'm just trying to hack some fields.
    – MariusCC
    Aug 19, 2009 at 9:14
1

There is a way to update ntext column:

DECLARE @memo binary(16)

SELECT 
    @memo = TEXTPTR(field1)
FROM 
    YourTable
WHERE 
    (your condition here)

UPDATETEXT YourTable.field1 @memo NULL 0 'Text to append'

Here are more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.