23

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Paul D. Eden
  • 19,939
  • 18
  • 59
  • 63
  • Off the top of my head I would say that should work. The issue is probably a conversion/casting issue. Try fieldname = fieldname + N'appended string' – Craig Jan 21 '09 at 17:57
  • Unfortunately, that didn't work. I got 'The data types text and nvarchar are incompatible in the add operator.' Thanks though. – Paul D. Eden Jan 21 '09 at 18:03

4 Answers4

40

Try this:

update 
  tablename
set
  fieldname = convert(nvarchar(max),fieldname) + 'appended string'
casperOne
  • 73,706
  • 19
  • 184
  • 253
Bravax
  • 10,453
  • 7
  • 40
  • 68
6

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
Joe
  • 41,484
  • 20
  • 104
  • 125
3

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

SQLMenace
  • 132,095
  • 25
  • 206
  • 225
0

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).

CharithJ
  • 46,289
  • 20
  • 116
  • 131
Todd
  • 439
  • 5
  • 5