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.
But in terms of just using SQL to add static text to a column in a SELECT statement, you can just concatenate the text directly in the statement. Same principle, just using an UPDATE instead of a SELECT , which will modify the underlying data instead of just modifying the view of the data.
Use UPDATETEXT to change only a part of a text, ntext, or image column in place. Use WRITETEXT to update and replace a whole text, ntext, or image field. This feature will be removed in a future version of Microsoft SQL Server.
Concatenates two strings and sets the string to the result of the operation. For example, if a variable @x equals 'Adventure', then @x += 'Works' takes the original value of @x, adds 'Works' to the string, and sets @x to that new value 'AdventureWorks'.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With