How To Update and Concatentate A string In MSSQL

Hi all,I have a problem here where I am trying to use SQL Parameters to update a column in the database, but the problem is that I need to concatenate the same column to some Text from an Application

This is the Code I Have, But it throws an exception

UPDATE rdm_comments SET commentinfo = commentinfo + 'additional info' where commentid=2356

I get an Error stating that i cannot use the addition operator for a string. Help

[516 byte] By [kleer242] at [2007-12-25]
# 1
Most likely your commentinfo column is a text field which you cannot concatenate other string to it. If the field is a varchar(4000), you can do it. You need to find a data type for the column which you can do this sort of operation.
limno at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2
Yes, the column commentinfo is a text field. I will concatenate in the application domain. I was hoping to save some time by doing it in SQL. Oh Well, Thanks!
kleer242 at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3

DECLARE @ptr varbinary(16)
DECLARE @len int

select @ptr = TEXTPTR(commentinfo), @len=datalength(commentinfo)/2 from rdm_comments where commentid=2356

UPDATETEXT rdm_comments.commentinfo @ptr @len 0 'additional info'

Note, in my example length of commentinfo is divided by 2 because I assumed commentinfo has ntext data type. If it has text data type then remove dividing.

AlexanderPavlov at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 4

In SQL Server 2005, you can use one of the new large value data types for this column: for example NVARCHAR(MAX).

You can run this query by using (.WRITE ( expression, @Offset , @Length )) for this column:

Update rdm_comments

set commentinfo.write('additional info',len(commentinfo),1)

WHERE commentid=2356

--OR

Update rdm_comments

set commentinfo.write('additional info',null,null )--append to the end

WHERE commentid=2356

limno at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 5
"... len(commentinfo) ..."

Does MSSQL2005 support len() function for ntext/text columns?

AlexanderPavlov at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 6

No. but this one: DATALENGTH(columnname).

The one I was using is for NVARCHAR(MAX) if choose to use the .WRITE function for update.

limno at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 7

Try This:

UPDATE Table SET Text = (Convert(varchar(8000), Text)+' my extra text')

Flyingbertman at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified