Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 update value in table that is type text

Author  Topic 

TRACEYSQL
Aged Yak Warrior

594 Posts

Posted - 2014-01-16 : 22:53:47
I tried to replace a value in table.

The column is text

How to do this, I tried updatetext

All that I want, is to update part of a string, using replace and update it, my replace is valid, it fails on the type column text.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-17 : 02:43:05
just cast it to varchar(max) and do the replace

see example below

declare @t table
(
t text
)
insert @t
values('long text containing <string to be replaced> and lots of other words separated by space character')

update @t
set t =replace(cast(t as varchar(max)),'<string to be replaced>','<new string value>')

select * from @t

output
------------------------------------------
t
------------------------------------------
long text containing <new string value> and lots of other words separated by space character



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2014-01-17 : 07:13:37
Please remember from BOL for UPDATETEXT "This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use the large-value data types and the .WRITE clause of the UPDATE statement instead."

I would pull the value out of the table to a varchar(max) make the change and use WRITETEXT to put it back.

djj
Go to Top of Page
   

- Advertisement -