Concatenate ntext in SQL Server 2005

放肆的年华 提交于 2019-12-08 15:58:23

问题


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?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/1272416/concatenate-ntext-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!