NickHoward
asked on
Removing part of text in column based on find and replace query
I have table called products and field cdescription where my data is mix of text and some wiered tags which I want to remove.
For example, a record looks like:
cdescription = This is best book <input id="gwProxy" type="hidden">
I want this to become:
cdescription = This is best book
Similary query should remove <input id="gwProxy" type="hidden"> from all records in cdescription without disturbing text data.
Thanks for your help.
For example, a record looks like:
cdescription = This is best book <input id="gwProxy" type="hidden">
I want this to become:
cdescription = This is best book
Similary query should remove <input id="gwProxy" type="hidden"> from all records in cdescription without disturbing text data.
Thanks for your help.
This will remove tags from your texts:
UPDATE tbl SET col = stuff(col, charindex('<',col), charindex('>',col)-charind ex('<',col )+1,'')
WHERE col IS NOT NULL and charindex('<',col) > 0 AND charindex('>',col) > charindex('<',col)
UPDATE tbl SET col = stuff(col, charindex('<',col), charindex('>',col)-charind
WHERE col IS NOT NULL and charindex('<',col) > 0 AND charindex('>',col) > charindex('<',col)
ASKER
I tried it:
UPDATE products3 SET cdescription = stuff(cdescription , charindex('<',cdescription ), charindex('>',cdescription )-charindex('<',cdescripti on )+1,'')
WHERE cdescription IS NOT NULL and charindex('<',cdescription ) > 0 AND charindex('>',cdescription ) > charindex('<',cdescription )
But got SQL error:
37000(8116)[Microsoft][ODB C SQL Server Driver][SQL Server]Argument data type text is invalid for argument 1 of stuff function.
37000(8180)[Microsoft][ODB C SQL Server Driver][SQL Server]Statement(s) could not be prepared. (0.00 secs)
Pls advice.
UPDATE products3 SET cdescription = stuff(cdescription , charindex('<',cdescription
WHERE cdescription IS NOT NULL and charindex('<',cdescription
But got SQL error:
37000(8116)[Microsoft][ODB
37000(8180)[Microsoft][ODB
Pls advice.
cdescription is text, so the STUFF does not support it. What SQL version do you have?
ASKER
SQL 2005
This works on SQL 2005:
UPDATE products3 SET cdescription = stuff(CAST(cdescription AS varchar(max)) , charindex('<',cdescription ), charindex('>',cdescription )-charindex('<',cdescripti on )+1,'')
WHERE cdescription IS NOT NULL and charindex('<',cdescription ) > 0 AND charindex('>',cdescription ) > charindex('<',cdescription )
If you have Unicode characters in the text column you should use nvarchar.
UPDATE products3 SET cdescription = stuff(CAST(cdescription AS varchar(max)) , charindex('<',cdescription
WHERE cdescription IS NOT NULL and charindex('<',cdescription
If you have Unicode characters in the text column you should use nvarchar.
ASKER
Thanks. Now there is no error. However the text i want to remove
<input id="gwProxy" type="hidden">
did not remove from the cdescription field data.
Am I missing something?
<input id="gwProxy" type="hidden">
did not remove from the cdescription field data.
Am I missing something?
Do you have max one or more occurences of this text in cdescription?
It works for me. If you have just several occurences of this text in each cdescription simply execute the UPDATE several times. If you have many occurences in each cdescription then it would be better to do it in a loop.
ASKER
You are absolutely right. I have several occurences. Wonder how we can loop it?
Thanks.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, I wrote "not slow" but it should be "slow" ...
ASKER
Helped to some extent. Thanks.
UPDATE urTable
SET TextCol = LEFT (Textcol, CHARINDEX('<', Textcol )-1 )
WHERE TextCol is not null