Find and replace LIKE sql data
I am trying to run a search and replace a query with some SQL data using Management Studio. I basically want to remove the word FREE from any content.
I tried this request;
UPDATE Table_1
SET ContentDetails = REPLACE(ContentDetails, 'FREE', '')
WHERE (ContentDetails LIKE '%FREE%')
But I am getting a message that the data type text is not valid for argument 1 of the replace function.
source to share
Since you have a column text
, you will need to use updatetext
, which is painful at best. However, you can use contentdetails
both varchar(max)
and you will be peaches.
update table_1
set contentdetails = replace(cast(contentdetails as varchar(max)), 'FREE', '')
where contentdetails like '%FREE%'
Also, I highly recommend that you translate this column from text
to varchar(max)
. It, along with ntext
and image
, is currently a deprecated data type that will be removed at some point in the future by SQL Server.
source to share