4

I would like to replace the text of a column in a table

I tried out:

select replace([article], '<p>&nbsp;</p>', '') from Articles

update Articles
set article = replace(article, '<p>&nbsp;</p>', '')
where article like '<p>&nbsp;</p>'

or 

UPDATE [AJA].[dbo].[Articles]
   SET [article] = ' '
 WHERE [article] = '<p>&nbsp;</p>'
GO

and everytime it comes out with the error:

argument 1 not valid in replace

What's wrong with it?

Thanks for your help

6
  • What is the actual SQL Server error message? Can't see that in SELECT * FROM sys.messages WHERE text LIKE '%argument%not valid in%' Commented Aug 31, 2012 at 9:01
  • 4
    what is the datatype of article? Commented Aug 31, 2012 at 9:01
  • 1
    And those statements all do different things - the first should show you the correct data, but won't update it in the database; the second updates the article column removing the string you specify only on rows where article equals exactly <p>&nbsp;</p> (you're missing the wildcard characters on your like statement); the third one sets the article body equal to a space where article matches <p>&nbsp;</p> exactly. Commented Aug 31, 2012 at 9:04
  • is it safe to assume that article is of datatype text ? Commented Aug 31, 2012 at 9:05
  • 2
    If it is text datatype the full error message should tell you the problem at least on 2008 Argument data type text is invalid for argument 1 of replace function. Commented Aug 31, 2012 at 9:06

3 Answers 3

6

I've check out your problem verifying with two datatype i.e.

  1. ntext : while working with ntext , it throws above error....Check out here

  2. varchar(max): While working with varchar(max), it is perfectly workin....Check out here

So, use varchar(max) datatype while working with html tag....

If you want to work on your previous type, then cast the column type as varchar

   SELECT REPLACE(CAST([article] as VARCHAR(MAX)), '<p>&nbsp;</p>', '')
   FROM Articles
Sign up to request clarification or add additional context in comments.

2 Comments

Good to know :), but I cannot change the design of the table, thanks
Go with updated answer....Cast the column type into varchar while using query....
3

You're getting this error because you've text datatype. With varchar datatype your query works fine.

You need to cast your field from text to varchar in order to use replace function.

Declare @mytable table
(
Article text
);

INSERT into @mytable VALUES('<p>&nbsp;</p>');
INSERT into @mytable VALUES('<p>&nbsp;</p>');
INSERT into @mytable VALUES('<p>&nbsp;</p>');
INSERT into @mytable VALUES('<b>&nbsp;</b>');


select replace(cast([article] as VARCHAR(8000)),'<p>&nbsp;</p>','')
from   @mytable
where Article LIKE '<p>&nbsp;</p>'

1 Comment

of course, this solution will cut off text longer than 8000 chars
0

Try this one

UPDATE Articles SET article = REPLACE(article, '<p>&nbsp;</p>', '')

Work on the other replaces likewise . .

2 Comments

This will update all rows even those where the text doesn't exist. Read @Bridge's comment
Hi coosal, doesn' t work on me, same error invalid argument 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.