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 ApplicationThis 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]
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.
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!
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.
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