简体   繁体   中英

How to append to a text field in t-sql SQL Server 2005

What is the best way to append to a text field using t-sql in Sql Server 2005?

With a varchar I would do this.

update tablename set fieldname = fieldname + 'appended string'

But this doesn't work with a text field.

Try this:

update 
  tablename
set
  fieldname = convert(nvarchar(max),fieldname) + 'appended string'

This should work (link)

Copied from link:

DECLARE @ptrval binary(16)
SELECT @ptrval = TEXTPTR(ntextThing)
FROM item
WHERE id =1
UPDATETEXT table.ntextthing @ptrval NULL 0 '!'
GO

in 2005 you should use varchar(max) or nvarchar(max) these columns will work with normal varchar functions. Text and ntext have been deprecated

The max length for varchar(max) is 2,147,483,647 characters. This is the same as the Text data type.

Whatever text could hold, this can hold, so you don't need to worry about running out of room by switching to VARCHAR(MAX).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM