Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 270
Paul D. Eden Avatar asked Jan 21 '09 17:01

Paul D. Eden


People also ask

How do you append a text column in SQL?

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.

How do I update a text field in SQL?

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.

How do I concatenate a string to a variable in 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'.


2 Answers

Try this:

update    tablename set   fieldname = convert(nvarchar(max),fieldname) + 'appended string' 
like image 85
Bravax Avatar answered Oct 20 '22 17:10

Bravax


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 
like image 37
Joe Avatar answered Oct 20 '22 17:10

Joe